2011-09-09 23 views
1

私は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 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))))) 

と(私はhereを提供するチュートリアルを次てるので)、私は機能の組み合わせとして、これを実現するために着手し、これは、私は次のようになってしまったために:

; 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) 

答えて

2

あなたarea-cylinderが間違っている: はテストとして、サイトには、次のコードを提供します。 円周をとし、高さを掛けます。したがって:

(define (area-cylinder radius height) 
    (* 2 pi radius height)) 

area-circleも間違っています。したがって、次のようになります。

(define (area-circle radius) 
    (* pi radius radius)) 

のでarea-pipe関数は次のようになります。

(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)))))