ringtaroのHaskell日記

2010年02月07日(日)

プログラミングHaskell第4章練習問題をやったよ。 22:51

1.


halve :: [a] -> ([a], [a])
halve xs = ( take cnt xs, drop cnt xs )
    where
        cnt = length xs `div` 2

2.


-- 2.a
safetail_a :: [a] -> [a]
safetail_a xs = if (null xs) then []
                             else drop 1 xs

-- 2.b
safetail_b :: [a] -> [a]
safetail_b xs | null xs   = []
              | otherwise = drop 1 xs

-- 2.c
safetail_c :: [a] -> [a]
safetail_c []     = []
safetail_c (x:xs) = xs

3.


(v) :: Bool -> Bool -> Bool
True  v True  = True
True  v False = True
False v True  = True
False v False = False

(v) :: Bool -> Bool -> Bool
False v False = False
_     v _     = True

(v) :: Bool -> Bool -> Bool
False v b  = b
_  v _     = True

(v) :: Bool -> Bool -> Bool
b v c | b == c    = b
      | otherwise = b

4.


and1 :: Bool -> Bool -> Bool
and1 x y = if (x == True) then
               if (y == True) then True
                              else False
           else
               False

5.


and2 :: Bool -> Bool -> Bool
and2 x y = if (x == True) then y
                          else False

6.


mult = ¥x -> ¥y -> ¥z -> x*y*z