len [] = 0 len (_:xs) = 1 + len xs hd (x:_) = x -- append xs ys = xs ++ ys append [] ys' = ys' append xs' [] = xs' append (x:xs) ys' = x : (append xs ys') -- id x = x idl [] = [] idl (x:xs) = (x : idl xs) undex :: [a] -> Int -> a -- undex xs n = xs !! n undex [] n = error "no undex at that value" undex (_:xs) (n+1) = undex xs n undex (x:_) 0 = x philter :: (a -> Bool) -> [a] -> [a] philter p [] = [] philter p (x:xs) = (if p x then (x:) else id) (philter p xs) mapp :: (a -> b) -> [a] -> [b] mapp f [] = [] mapp f (x:xs) = f x : mapp f xs unz :: [ (a,b) ] -> ( [a],[b] ) unz [] = ( [] ,[] ) unz ( x :xs) = ( fst x : fst (unz xs) , snd x : snd (unz xs) ) unz ((a,b):xs) = ( a : fst (unz xs) , b : snd (unz xs) ) unz ((a,b):xs) = ( a : fst u , b : snd u ) where u = unz xs unz ((a,b):xs) = ( a : ua , b : ub ) where (ua,ub) = unz xs unz ((a,b):xs) = (a:ua, b:ub) where (ua,ub) = unz xs ----------------------- elem' x [] = False elem' x (y:ys) = if x==y then True else elem' x ys peek x [] = Nothing peek x ((y,z):ps) = if x==y then Just z else peek x ps fold :: (a -> b -> b) -> b -> [a] -> b fold f r [] = r fold f r (x:xs) = f x (fold f r xs) f b n = if b then n else 2*n rev [] = [] rev (x:xs) = rev xs : (x:[]) ------------------------ count :: (a->Bool) -> [a] -> Int -- count p [] = 0 -- count p (x:xs) = (if p x then (1+) else id) (count p xs) -- count p xs = length (filter p xs) -- count p xs = (length . filter p) xs count p = length . filter p -- stutter n xs = concat (replicate n xs) -- stammer n xs = concat (map (replicate n) xs) stutter n = concat . replicate n stammer n = concat . map (replicate n) tkot n = stutter n . stammer n