Racket 球拍基本问题
我刚开始玩球拍,(作为一个新手)我很难找出我的代码到底出了什么问题。起初,我尝试将其作为单个函数实现,但效果很好:Racket 球拍基本问题,racket,Racket,我刚开始玩球拍,(作为一个新手)我很难找出我的代码到底出了什么问题。起初,我尝试将其作为单个函数实现,但效果很好: ; Finds surface area of pipe ; outside surface area (2pir+thickness)*length ; inside SA 2pirad*length ; 2 (area of outer circle - area of inner circle) ; add all together (define (area-pipe in
; Finds surface area of pipe
; outside surface area (2pir+thickness)*length
; inside SA 2pirad*length
; 2 (area of outer circle - area of inner circle)
; add all together
(define (area-pipe inner_radius height thickness)
(+ (* 2 pi inner_radius height)
(* 2 pi height (+ inner_radius thickness))
(- (* 2 pi (sqr (+ inner_radius thickness)))
(* 2 pi (sqr inner_radius)))))
并且(由于我遵循提供的教程),我开始将其作为一个功能组合来实现,最后我总结了以下内容:
; functional implementation
(define (area-circle radius)
(* 2 pi (sqr radius)))
(define (area-cylinder radius height)
(* 2 pi (sqr radius) height))
;actual function--why doesn't this quite work as planned?
(define (area-pipe1 inner_radius height thickness)
(+ (area-cylinder inner_radius height)
(area-cylinder (+ inner_radius thickness) height)
(- (area-circle (+ inner_radius thickness))
(area-circle inner_radius))))
所以,我猜我的定义有问题。然而,我希望能得到一些提示和提示,说明为什么我没有得到正确的答案。
作为测试,站点提供以下代码:
(test (area-pipe1 0.5 0 0.5) 4.71)
(test (area-pipe1 0 2 1) 18.85)
(test (area-pipe1 1 2 1) 56.54)
您的
区域圆柱体
错误。它应该取周长乘以高度。因此:
(define (area-cylinder radius height)
(* 2 pi radius height))
您的面积圆
也是错误的。因此,应该是:
(define (area-circle radius)
(* pi radius radius))
因此,区域管道
功能应为:
(define (area-pipe2 inner-radius height thickness)
(+ (area-cylinder inner-radius height)
(area-cylinder (+ inner-radius thickness) height)
(* 2 (- (area-circle (+ inner-radius thickness))
(area-circle inner-radius)))))