module MyFile where import Char ------------------- -- MyFile.hs: a sample file for students of CS 254 -- by Fritz Ruehr, WU, Spring 2009 ------------------- -- f a = (map toUpper (take 1 a)) ++ tail a f' a = ( toUpper (head a)) : tail a title a = unwords (map f' ( map (map toLower) (words a))) -- here is my very special test data test = "thIS IS a TEST phrase to be TItleD" title' s = unwords (map capitalize (words (map toLower s))) where capitalize s = toUpper (head s) : tail s dv xs = filter novowel xs where novowel x = notElem x vs && notElem x capvs vs = "aeiou" -- lowercase vowels capvs = map toUpper vs trim s = reverse (dropWhile isSpace (reverse (dropWhile isSpace s))) trim' = trimSpace . fromBack trimSpace where trimSpace = dropWhile (==' ') fromBack f = reverse . f . reverse dog = "the dog runs" f = unwords . map reverse . words g = unwords . reverse . words orr = foldr (||) False eny p xs = orr (map p xs) awl p xs = foldr (&&) True (map p xs) dbase = [ ("Janeba", 305), ("Johnson", 301), ("Otto", 303) ] zap as bs = take n (zip (ouro as) (ouro bs)) where n = length as `max` length bs ouro xs = xs ++ repeat (last xs) flap f x y = f y x unkurri f (x,y) = f x y ---------------------------------------- fold f u [] = u fold f u (x:xs) = f x (fold f u xs) -- len = fold (const (1+)) 0 summ = fold (+) 0 prod = fold (*) 1 fac 0 = 1 fac (n+1) = (n+1) * fac n fib 0 = 1 fib 1 = 1 fib (n+2) = fib (n+1) + fib n fibs = 1 : 1 : zipWith (+) fibs (tail fibs) fibb i = fibs !! i ------------------------ data Nat = Zero | Succ Nat deriving Show add Zero m = m add (Succ n) m = Succ (add n m) mul Zero m = Zero mul (Succ n) m = add m (mul n m) pow m Zero = Succ Zero pow m (Succ n) = mul m (pow m n) iter s z Zero = z iter s z (Succ n) = s (iter s z n) plus n m = iter Succ m n mult n m = iter (plus m) Zero n expt n m = iter (mult m) one n ntoi Zero = 0 ntoi (Succ n) = 1 + (ntoi n) n2i = iter (+1) 0 i2n = yttr Succ Zero iton 0 = Zero iton (i+1) = Succ (iton i) yttr s z 0 = z yttr s z (i+1) = s (yttr s z i) lom = yttr (():) [] lomp k l = k ++ l lomm k l = concat (map (const k) l) l2i = length i2l = flip replicate () att f i j = l2i (f (i2l i) (i2l j)) try f i j = ntoi (f (iton i) (iton j)) one = Succ Zero two = Succ (Succ Zero) three = Succ two data Unit = Unit ---------------------------------- -- If you missed the "reverse" lecture, see >> HERE << ---------------------------------- -- lists define by us instead of Haskell's built-in lists: data List a = Nil | Cons a (List a) deriving Show -- a version of length for "our" lists len Nil = 0 len (Cons x xs) = 1 + len xs -- a version of map for "our" lists mapp :: (a -> b) -> List a -> List b mapp f Nil = Nil mapp f (Cons a as) = Cons (f a) (mapp f as) -- a version of filter for "our" lists phylter :: (a -> Bool) -> List a -> List a phylter p Nil = Nil phylter p (Cons x xs) = if p x then Cons x (phylter p xs) else phylter p xs -- a version of append for "our" lists apend Nil ys = ys apend (Cons x xs) ys = Cons x (apend xs ys) -- first version of reverse for "our" lists -- (too slow: since it uses "apend", it is O(n^2) ) riverse Nil = Nil riverse (Cons x xs) = (riverse xs) `apend` (Cons x Nil) -- a better version of "riverse", using an *accumulating parameter* (acc) -- (see also the graphic linked in from the home page) friv acc Nil = acc friv acc (Cons x xs) = friv (Cons x acc) xs -- changing the order of arguments to friv phriv Nil acc = acc phriv (Cons x xs) acc = phriv xs (Cons x acc) -- phriv above can now be re-written to show more directly that -- we are constructing a function on lists phrivv Nil = id phrivv (Cons x xs) = phrivv xs . Cons x -- conversion from Haskell lists to ours l2L [] = Nil l2L (x:xs) = Cons x (l2L xs) -- a "copying" indentity function on Lists idd Nil = Nil idd (Cons x xs) = Cons x (idd xs) -- sample List for testing tl = l2L [1..1000] ------------------------------ factors n = [ x | x<- [1..n], n `mod` x == 0 ] konkat xss = [ x | xs <- xss, x <- xs ]