module Chap3 where { fact :: Integer -> Integer; fact n = if n==0 then 1 else n*fact(n-1); {- Preludeに定義済み length :: [a] -> Integer; length [] = 0; length (x:xs) = 1 + length xs; -} append :: [a] -> [a] -> [a]; append [] ys = ys; append (x:xs) ys = x : (append xs ys); {- Preludeに定義済み reverse :: [a] -> [a]; reverse [] = []; reverse (x:xs) = append (reverse xs) [x]; -} -- shunt は revの補助関数 shunt :: [a] -> [a] -> [a]; shunt ys [] = ys; shunt ys (x:xs) = shunt (x:ys) xs; rev :: [a] -> [a]; rev xs = shunt [] xs; {- Preludeに定義済み zip :: [a] -> [b] -> [(a,b)]; zip (a:as) (b:bs) = (a,b) : zip as bs; zip _ _ = []; -} pow4 x = let { y = x*x } in y*y; head ys = let { (x:xs) = ys } in x; repeat :: a -> [a]; repeat x = let { xs = x:xs } in xs; data Direction = Up | Down | Left | Right; data Tree a = Branch (Tree a) a (Tree a) | Empty; from :: Integer -> [Integer]; from n = n : from (n+1); {- Preludeに定義済み take :: Integer -> [a] -> [a]; take 0 _ = []; take _ [] = [] ; take n (x:xs) = x : take (n-1) xs; -} unit :: a -> [a]; unit a = [a]; bind :: [a] -> (a -> [b]) -> [b]; bind [] _ = []; bind (x:xs) f = append (f x) (bind xs f) }