import SimplePrelude hiding (length, last, head, tail, concat, fst) -- Compute the length of a list of integers: lengthInt :: [Int] -> Int lengthInt [] = 0 lengthInt (x : xs) = 1 + lengthInt xs -- Compute the length of a list of floats: lengthFloat :: [Float] -> Int lengthFloat [] = 0 lengthFloat (x : xs) = 1 + lengthFloat xs -- Compute the length of a list of characters: lengthChar :: [Char] -> Int lengthChar [] = 0 lengthChar (x : xs) = 1 + lengthChar xs -- Compute the length of a list: length :: [a] -> Int length [] = 0 length (x : xs) = 1 + length xs -- (++) :: [a] -> [a] -> [a] -- Computes the last element of a list: last :: [a] -> a last [x] = x last (x : xs) = last xs -- Maybe there is some value of type 'a': --data Maybe a = Nothing | Just a noValue :: Maybe a noValue = Nothing aOne :: Maybe Int aOne = Just 1 isNothing :: Maybe a -> Bool isNothing Nothing = True isNothing (Just x) = False -- Binary trees with values of type 'a' at the leaves: data Tree a = Leaf a | Node (Tree a) (Tree a) deriving Show tree1234 :: Tree Int tree1234 = Node (Leaf 1) (Node (Node (Leaf 2) (Leaf 3)) (Leaf 4)) -- Height of a tree: number of nodes of the longest branch height :: Tree a -> Int height (Leaf x) = 0 height (Node t1 t2) = 1 + max (height t1) (height t2) -- Some selector functions on lists: head :: [a] -> a -- head [] ???? head (x:xs) = x tail :: [a] -> [a] tail (x:xs) = xs -- Computes the last element of a list: maybeLast :: [a] -> Maybe a maybeLast [] = Nothing maybeLast [x] = Just x maybeLast (x : xs) = maybeLast xs -- Computes the n-th element of a list (n=0: head of the list) nth :: [a] -> Int -> a nth (x:xs) n = if n == 0 then x else nth xs (n - 1) -- predefined as (!!) hello :: String --hello = ['H','e','l','l','o'] hello = "Hello" -- Union type: -- data Either a b = Left a | Right b anIntStringList :: [Either Int String] anIntStringList = [Left 1, Right "Hello", Left 2, Right "World"] allInts :: [Either Int a] -> [Int] allInts [] = [] allInts (Left n : xs) = n : allInts xs allInts (Right x : xs) = allInts xs aPair :: (Int,String) aPair = (42, "Meaning") aTriple :: (Int,Bool,Char) aTriple = (3,True,'\n') -- Compute the first component of a pair: fst :: (a,b) -> a fst (x,y) = x