Mercurial > hg > Members > masakoha > testcode
diff Haskell/baby.hs @ 26:0f4ccdbaf57f
add mmap
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 May 2014 03:49:24 +0900 |
parents | 8c7e1b34582f |
children |
line wrap: on
line diff
--- a/Haskell/baby.hs Wed Mar 26 18:13:52 2014 +0900 +++ b/Haskell/baby.hs Sun May 11 03:49:24 2014 +0900 @@ -48,3 +48,60 @@ ++ case ls of [] -> "empty." [x] -> "a singleton list." xs -> "a longer list." + +maximum' :: (Ord a) => [a] -> a +maximum' [] = error "maximum of empty list!" +maximum' [x] = x +maximum' (x:xs) = max x (maximum' xs) + +replicate' :: Int -> a -> [a] +replicate' n x + | n <= 0 = [] + | otherwise = x : replicate' (n-1) x + +take' :: Int -> [a] -> [a] +take' n _ + | n <= 0 = [] +take' _ [] = [] +take' n (x:xs) = x : take' (n-1) xs + +reverse' :: [a] -> [a] +reverse' [] = [] +reverse' (x:xs) = reverse' xs ++ [x] + +repeat' :: a -> [a] +repeat' x = x : repeat' x + +quicksort :: (Ord a) => [a] -> [a] +quicksort [] = [] +quicksort (x:xs) = + let smallerOrEqual = [a| a<-xs, a<= x] + larger = [a | a<-xs, a>x] + in quicksort smallerOrEqual ++ [x] ++ quicksort larger + +-- Chapter 5 + +multThree :: Int -> Int -> Int -> Int +multThree x y z = x * y * z + +compareWithHundred :: Int -> Ordering +compareWithHundred x = compare 100 x + +compareWithHundred' :: Int -> Ordering +compareWithHundred' = compare 100 + +zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c] +-- 型推論をさせると zipWith' :: (t -> t1 -> a) -> [t] -> [t1] -> [a] となる +zipWith' _ [] _ = [] +zipWith' _ _ [] = [] +zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys + +flip' :: (a -> b -> c) -> (b -> a -> c) +flip' f = g + where g x y = f y x + +chain :: Integer -> [Integer] +chain 1 = [1] +chain n + | even n = n : chain (n `div` 2) + | odd n = n : chain (n * 3 + 1)