module CS254 where
import Char
--------------------
-- Some examples for early CS 254
-- Fritz Ruehr * Willamette * Spr 08
-- the Fibonacci numbers (as an infinite list)
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
infinity = 1 + infinity
n = n
-- the prime numbers (as an infinite list)
primes = sieve [2..]
sieve (n:ns) = n : sieve (filter ndiv ns)
where ndiv k = k `mod` n /= 0
-- a quicksort program
qsort [] = []
qsort (x:xs) = qsort [ sm | sm <- xs, sm < x ] ++ [x] ++
qsort [ lg | lg <- xs, lg >= x ]
qs [] = []
qs (x:xs) = rec (<) ++ x : rec (>=)
where rec p = qs (filter (`p` x) xs)
-- a program to test for palindromes
pal str = (new == reverse new)
where new = norm str
norm = map toLower . filter isAlpha
-- some palindromes for testing
tests = [ "Madam, in Eden, I'm Adam." ,
"A man, a plan, a canal ... Panama!" ,
"" ,
"A dog, a plan, a canal: pagoda." ,
"Are we not drawn onward to new era?" ,
"Cigar? Toss it in a can. It is so tragic." ,
"Ed, I saw Harpo Marx ram Oprah W. aside." ,
"No misses ordered roses, Simon." ,
"A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal - Panama!",
"does it just work for everything?"
]
palword = "You can cage a swallow, can't you, but you can't swallow a cage, can you?"