Hatena::Groupsicp

yharaの日記

参考リンク
 | 

2007-05-03

[].3 15:34

テストファーストするよ。

1.3.scm:

(define (sum-of-squares-of-biggest-two a b c)
  0)

test-1.3.scm:

(use gauche.test)
(load "./1.3.scm")

(test-start "1.3")
(test* "test1"
       13
       (sum-of-squares-of-biggest-two 1 2 3))
(test* "test2"
       (+ 25 36)
       (sum-of-squares-of-biggest-two 6 4 5))
(test-end)

実行結果:

yhara@meteor:~/src/sicp % gosh test-1.3.scm
Testing 1.3 ...
test test1, expects 13 ==> ERROR: GOT 0
test test2, expects 61 ==> ERROR: GOT 0
failed.
discrepancies found.  Errors are:
test test1: expects 13 => got 0
test test2: expects 61 => got 0

正しく失敗した模様。

1.3.scmをちゃんと実装するよ。

(define (sum-of-squares-of-biggest-two a b c)
  (let1 sorted (sort (list a b c))
        (+ (expt (car sorted) 2)
           (expt (cadr sorted) 2))))

こんなんでどうでしょ。(あんまり、綺麗でも速そうでもないけど…。) exptは累乗ね。

yhara@meteor:~/src/sicp % gosh test-1.3.scm
Testing 1.3 ...
test test1, expects 13 ==> ERROR: GOT 5
test test2, expects 61 ==> ERROR: GOT 41
failed.
discrepancies found.  Errors are:
test test1: expects 13 => got 5
test test2: expects 61 => got 41

ありゃ?

ああ、sortは小さい順に並ぶんだっけ。

(define (sum-of-squares-of-biggest-two a b c)
  (let1 sorted (sort (list a b c) >)
        (+ (expt (car sorted) 2)
           (expt (cadr sorted) 2))))

これでどうだ。

yhara@meteor:~/src/sicp % gosh test-1.3.scm
Testing 1.3 ...
test test1, expects 13 ==> ok
test test2, expects 61 ==> ok
passed.

OKのようです。

ChristianaChristiana2012/01/10 09:33That's more than snebsile! That's a great post!

sgiodkkfjgssgiodkkfjgs2012/01/10 19:14Gv8tkW <a href="http://cjqpfjauqswr.com/">cjqpfjauqswr</a>

ntfhyvcwntfhyvcw2012/01/12 22:54TKFix7 <a href="http://egvshzpudncy.com/">egvshzpudncy</a>

 |