{- Answers to Haskell Mini Exercises #2 -} module Mini2 where import Data.Char {- The following are correct types for member: member :: (Ord a) => a -> [a] -> Bool member :: (Eq a) => a -> [a] -> Bool member :: (Eq a) => [a] -> [[a]] -> Bool member :: Bool -> [Bool] -> Bool This is the most general type for member: member :: (Eq a) => a -> [a] -> Bool -} mystery = 1 : map (*2) mystery {- mystery is the list of powers of 2: [1,2,4,8,16,32,64,128,256,512, ... -} {- infinite list of all integers, ordered in such a way that you can find any given integer after searching a finite number of elements -} ints = 0 : intsfrom 1 intsfrom n = n : (-n) : intsfrom (n+1) {- or an alternate version: -} otherints = 0: interleave [1..] [-1, -2 ..] interleave (x:xs) (y:ys) = x : y : interleave xs ys -- inorder and postorder tree traversal functions data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show,Read) inorder :: Tree a -> [a] inorder EmptyTree = [] inorder (Node x left right) = inorder left ++ [x] ++ inorder right postorder :: Tree a -> [a] postorder EmptyTree = [] postorder (Node x left right) = postorder left ++ postorder right ++ [x] -- List is just like the built-in list type, but without such a nice syntax data List a = Empty | Cell a (List a) deriving (Show,Read) append Empty ys = ys append (Cell x xs) ys = Cell x (append xs ys) mymap f Empty = Empty mymap f (Cell x xs) = Cell (f x) (mymap f xs) -- some example lists x = Cell 1 (Cell 2 (Cell 3 Empty)) y = Cell 10 (Cell 11 Empty) z = append x y m = mymap (*2) z {- print the square root of 2. Converted from printsqrt2 = do putStr "the square root of 2 is " putStrLn (show (sqrt 2)) -} printsqrt2 = putStr "the square root of 2 is " >> putStrLn (show (sqrt 2)) {- read in x and calculate its square root. Converted from: calcsqrt = do x <- readLn putStrLn "calculating the square root of x" putStrLn (show (sqrt x)) -} calcsqrt = readLn >>= \x -> putStrLn "calculating the square root of x" >> putStrLn (show (sqrt x)) capitalize = do s <- getLine putStrLn (map toUpper s)