-- FP1 lecture 7 March 2011

type Point = (Float,Float)

myfst :: (a,b) -> a
-- myfst (x,y) = x
myfst p = fst p  -- fst predefined

swap :: (a,b) -> (b,a)
-- swap (x,y) = (y,x)
swap p = (snd p, fst p) 

diag :: a -> (a,a)
diag x = (x,x)

add :: Point -> Point -> Point
add (x,y) (x',y') = (x + x', y + y')

myhead :: [a] -> a
myhead []     = error "head of empty"
myhead (x:xs) = x

exactlytwo :: [a] -> Bool
exactlytwo (x:y:[]) = True
exactlytwo _        = False

-- f 'a' 'd' = [('a',0),('b',1),('c'),2),('d',3)]

f :: Char -> Char -> [(Char,Int)]
f c d = let {
                cd = [c..d] ;
                k  = length cd -1;
                ns = [0..k] 
            }
        in zip cd ns

pyth :: Integer -> [(Integer,Integer,Integer)]
pyth n = [(x,y,z) | x <- ns, y <- ns, z <- ns, x^2 + y^2 == z^2, x <= y]

   where ns = [1..n]


pyth1 :: Integer -> [(Integer,Integer,Integer)]
pyth1 n = [(x,y,z) | x <- [1..n], 
                     y <- [x+1..n],
                     gcd x y == 1, 
                     z <- [y+1..n],
                     x^2 + y^2 == z^2]


three :: [String] -> [String]
three strs = [s | s <- strs, length s == 3]

eval12 :: (Float -> Float) -> Float
eval12 f = f 1 + f 2

myfun :: Float -> Float
myfun x = x^2 + x - 3

g :: Float -> Float
g = (+) 7
