Programming performance/Lemnext Haskell

From HaskellWiki
< Programming performance
Revision as of 12:43, 9 April 2009 by Lemnext (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
  • 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