f :: a -> a f = \ x -> x f' :: a -> a f' x = x -- Bool data MyBoolean = T | F deriving (Show) -- Unit data MyUnit = U deriving (Show) -- Either a b data MyEither a b = Left' a | Right' b deriving (Show) -- Empty data Empty abort :: Empty -> a abort x = error "abort" g :: Either (a -> Empty) b -> (a -> b) g x y = case x of Left z -> abort (z y) Right h -> h safeDiv :: Integer -> Integer -> Either String Integer safeDiv x y = if y == 0 then Left "div by zero" else Right (div x y) -- Product -- List -- Recursion. -- add' :: Int -> Int -> Int add1 :: Integer -> Integer -> Integer add1 0 y = y add1 x y | x > 0 = (add1 (x-1) y) + 1 add2 :: Integer -> Integer -> Integer add2 x y = if x == 0 then y else (add2 (x-1) y) + 1 add3 :: Integer -> Integer -> Integer add3 x y = case x == 0 of True -> y False -> (add3 (x-1) y) + 1 -- n! = n * (n-1) ... * 1 fac :: Integer -> Integer fac n = if n == 0 then 1 else n * fac (n-1) fac' :: Int -> Int fac' n = if n == 0 then 1 else n * fac' (n-1) -- "Monad" -- Computation types and do-notation. hello :: IO () hello = do putStrLn "welcome to haskell, what is your name?" l <- getLine putStrLn $ "welcome " ++ l ++ "!" return () hello' :: IO () hello' = do putStrLn "welcome to haskell, what is your name?" l <- getLine putStrLn $ "welcome " ++ l ++ "!" hello' main :: IO () main = hello'