-- 21 March 2011
square, times2, pol :: Float -> Float
square x = x^2
times2 x = x*2
pol x = x^2 + 2*x + 1
-- does square . times = timis2 . square ?
-- no!
stdev :: [Float] -> Float
stdev values = sqrt(sum (map f values) / n)
where
n = fromIntegral (length values)
m = sum values / n
f :: Float -> Float
f x = (x-m)^2
-- all inverses of non-zero elemnts
invs :: [Float] -> [Float]
invs = map (1/) . filter (/= 0)
-- alternative solutions:
-- invs xs = (map (1/) . filter (/= 0)) xs
-- invs xs = map (1/) (filter (/= 0) xs)
-- invs xs = map inv (filter notzero xs) where
-- inv x = 1/x
-- notzero x = x /= 0
-- invs xs = [1/x | x <- xs, x /= 0]
{-
*Main> invs [1,2,0,4,0,8]
[1.0,0.5,0.25,0.125]
-}
myzip :: [a] -> [b] -> [(a,b)]
myzip xs ys = zipWith f xs ys where
f x y = (x,y)
{-
*Main> myzip [1,2,3] [4,5,6]
[(1,4),(2,5),(3,6)]
*Main> zip [1,2,3] [4,5,6]
[(1,4),(2,5),(3,6)]
-}