Skip to content

Instantly share code, notes, and snippets.

@eisbaw
Created May 5, 2012 21:51
Show Gist options
  • Select an option

  • Save eisbaw/2605824 to your computer and use it in GitHub Desktop.

Select an option

Save eisbaw/2605824 to your computer and use it in GitHub Desktop.
"Pipes" in Haskell
#!/usr/bin/env runhaskell
-- Single and double pipe operators are taken, so use triple-pipe :)
infixl 9 |||
(|||) :: a -> (a -> b) -> b
x ||| f = f x
len = fromIntegral . length
mean xs = sum xs / len xs
stddev1 xs = xs ||| map (subtract my) ||| map (^2) ||| sum ||| (/n) ||| sqrt
where my = mean xs;
n = len xs
stddev2 xs = sqrt $ (/n) $ sum $ map (^2) $ map (subtract my) $ xs -- Two maps in succession can be combined: map ((^2).subtract (mean xs))
where my = mean xs;
n = len xs
datapoints = [2,4,4,4,5,5,7,9]
my = mean datapoints
sigma = stddev1 datapoints
main = print (sigma)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment