Programming performance/Lemnext Haskell
- Language: Haskell
- Skill: Intermediate
- Time: 20 minutes
Code
import Data.List (partition)
main = readFile "gspc.txt" >>= \x->
let ys = map (read . last . words) $ filter ((/=) '#' . head) $ lines x in
print $ g $ foldr f ([],10000,0) ys
f a (xs,m,b) = if p b < -0.03
then ((m*0.1/a,a):ys,m*0.9+s,a)
else (ys,m+s,a) where
(ys,zs) = partition ((>) 0.06 . p . snd) xs
s = g (zs,0,a)
p x = (a-x)/x
g (xs,m,a) = foldl (\x (y,_)-> x+a*y) m xs