28 June, 2006

[Haskell] Exercise 3.10, 3.11


import Prelude hiding (max,min)
max :: Int -> Int -> Int
max x y
| x >= y = x
| otherwise = y
maxThree :: Int -> Int -> Int -> Int
maxThree x y z
| x >= y && x >= z = x
| y >= z = y
| otherwise = z
min:: Int -> Int -> Int
min x y
| x <= y = x
| otherwise = y
minThree :: Int -> Int -> Int -> Int
minThree x y z
| x <= y && x <= z = x
| y <= z = y
| otherwise = z

Main> :type max
max :: Int -> Int -> Int
Main> max (3-2) (3*8)
24
Main> maxThree (4+5) (2*6) (100 `div` 7)
14
Main>

max (3-2) (3*8)
x >= y = x をテスト
(3-2) >= (3*8)
1 >= 24
False
otherwise y にマッチ
24

maxThree (4+5) (2*6) (100 `div` 7)
x >=y && x>=z = xをテスト
(4+5) >= (2*6) && (4+5) >= (100 `div` 7)
9 >= 12 && 9 >= 14
False && False
y >= z = yをテスト
(2*6) >= (100 `div` 7)
12 >= 14
False
otherwise = zにマッチ
14

No comments: