01 July, 2006

[Haskell] Exercise 4.15 - 4.20

テストケースの自動生成を行うquickCheckについては、いつか。(現状ではテスト仕様を私がHaskellでうまく書けないので。)
○ Exercise 4.15
allEquals :: Int -> Int -> Int -> Bool
のテストなので、
allEquals 1 1 1, allEquals 1 1 2, allEquals 1 2 1, allEquals 2 1 1, allEquals 1 2 3
とかをテストする。

○ Exercise 4.16
allEqualsの実装solutionについて検討する。m+n=2p となるようなケース(最後のケース)でsolutionは誤る。

solution :: Int -> Int -> Int -> Bool
solution m n p = ((m+n+p)==3*p)

Main> solution 1 1 1
True
Main> solution 1 1 2
False
Main> solution 1 2 1
False
Main> solution 2 1 1
False
Main> solution 1 2 3
False
Main> solution 1 5 3
True

○ Exercise 4.17
allDifferent :: Int -> Int -> Int -> Bool
テストケースとしては、
allDifferent 1 1 1, allDifferent 1 1 2, allDifferent 1 2 1, allDifferent 2 1 1, allDifferent 1 2 3
とか。
○ Exercise 4.18
allDifferentの実装attemptについて。attempt 1 2 1で誤り。

attempt :: Int -> Int -> Int -> Bool
attempt m n p = (m /= n) && (n /= p)

Main> attempt 1 1 1
False
Main> attempt 1 1 2
False
Main> attempt 1 2 1
True
Main> attempt 2 1 1
False
Main> attempt 1 2 3
True
Main>

○ Exercise 4.19
関数自体はExercise3.14で作成している。
ケースとしては、
* 3つとも同じ値
* 平均以上が1つの場合。引数の入れ替えに対して対称。
* 平均以上が2つの場合。引数の入れ替えに対して対称。
を試す。

Main> howManyAboveAverage 1 1 1
0
Main> howManyAboveAverage 1 2 10
1
Main> howManyAboveAverage 1 10 2
1
Main> howManyAboveAverage 2 1 10
1
Main> howManyAboveAverage 2 10 1
1
Main> howManyAboveAverage 10 1 2
1
Main> howManyAboveAverage 10 2 1
1
Main> howManyAboveAverage 1 10 11
2
Main> howManyAboveAverage 1 11 10
2
Main> howManyAboveAverage 10 1 11
2
Main> howManyAboveAverage 10 11 1
2
Main> howManyAboveAverage 11 1 10
2
Main> howManyAboveAverage 11 10 1
2
Main>

○ Exercise 4.20
Exercise 4.14を参照の事。

No comments: