double :: Int -> Int
double x = x + x
quad x = double (double x)
fact1 :: Int -> Int
fact1 1 = 1
fact1 n = n * fact1 (n-1)
power :: Int -> Int -> Int
power n 0 = 1
power n k = n * power n (k-1)
and2 :: Bool -> Bool -> Bool
and2 True b = b
and2 False _ = False
head' (x:_) = x
tail' :: [a] -> [a]
tail' (_:xs) = xs
len [] = 0
len (_:xs) = 1 + len xs
last' :: [a] -> a
last' (x:[]) = x
last' (_:xs) = last' xs
first (x,_) = x
second :: (a,b) -> b
second (_,x) = x
type Pos = (Float,Float)
type Trans = Pos -> Pos
moveLeft :: Trans
moveLeft (x,y) = (x-1,y)
dist1 :: Pos -> Pos -> Float
dist1 (x1,y1) (x2,y2) =
let
dx = (x1-x2)
dy = (y1-y2)
in sqrt (dx^2 + dy^2)
sgn n | n < 0 = -1
| n == 0 = 0
| otherwise = 1
flat1 :: [[a]] -> [a]
flat1 xss = [x | xs <- xss, x <- xs]
factors n = [x | x <- [1..n], mod n x == 0]
prime n = factors n == [1,n]
primes = [ x | x <- [2..], prime x]
qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort [ a | a <- xs, a < x]
++ [x] ++
qsort [ a | a <- xs, a >= x]
add1 :: Int -> (Int -> Int)
add1 x y = x+y
add2 :: (Int,Int) -> Int
add2 (x,y) = (x+y)
type Pair a = (a,a)
copy :: a -> Pair a
copy x = (x,x)
mult :: Pair Int -> Int
mult (x,y) = x*y
data Answer = Yes | No | Unknown deriving Show
flip1 :: Answer -> Answer
flip1 Yes = No
flip1 No = Yes
flip1 Unknown = Unknown
data Shape = Circle Float | Rect Float Float deriving Show
square :: Float -> Shape
square x = Rect x x
area :: Shape -> Float
area (Circle r) = pi*r^2
area (Rect x y) = x*y
data MMaybe a = MNothing | MJust a deriving Show
safehead :: [a] -> MMaybe a
safehead [] = MNothing
safehead (x:_) = MJust x
find :: (Eq k) => k -> [(k,v)] -> Maybe v
find k [] = Nothing
find k ((k1,v1):rest) | k==k1 = Just v1
| otherwise = find k rest
data Nat = Zero | Succ Nat deriving Show
nat2int :: Nat -> Int
nat2int Zero = 0
nat2int (Succ n) = 1 + nat2int n
int2nat :: Int -> Nat
int2nat 0 = Zero
int2nat n = Succ (int2nat (n-1))
add3 :: Nat -> Nat -> Nat
add3 Zero n = n
add3 (Succ n) m = Succ (add3 n m)