Created
May 5, 2012 21:51
-
-
Save eisbaw/2605824 to your computer and use it in GitHub Desktop.
"Pipes" in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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