HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Programming performance/Christoph Haskell

< Programming performance

Code

module Main where
 
data Values = Values { money, lastCosts :: Double
                     , stock            :: [(Double, Double)] }
 
readClosingCosts :: String -> Double
readClosingCosts = read . last . words
 
notComment ('#':_) = False
notComment _       = True
 
readCosts :: IO [Double]
readCosts = readFile "gspc.txt" >>=
  (return . map readClosingCosts . filter notComment . lines)
 
buy :: Values -> Double -> Double -> Values
buy v c amount = Values { money = (1 - amount) * money v
                        , stock = (amount * (money v) / c, c) : stock v
                        , lastCosts = c }
 
sellAll costs (bought, _) = bought * costs
 
sellFinally :: Double -> Values -> Double
sellFinally costs v = money v + (sum $ map (sellAll costs) (stock v))
 
tooMuchUp costs (bought, origCosts) = costs >= 1.06 * origCosts
 
step1 costs v
    | costs <= 0.97 * lastCosts v = buy v costs 0.1
    | otherwise                   = v { lastCosts = costs }
 
step2 costs v = v { money = money v + newMoney
                  , stock = filter (not . tooMuchUp costs) (stock v) }
    where newMoney = sum $ map (sellAll costs)
                               (filter (tooMuchUp costs) (stock v))
 
step :: Double -> Values -> Values
step costs v = step2 costs (step1 costs v)
 
main = do
  costs <- readCosts
  print $ sellFinally (head costs) $ foldr step (Values 10000 0 []) costs

Retrieved from "http://haskell.org/haskellwiki/Programming_performance/Christoph_Haskell"

This page has been accessed 289 times. This page was last modified 14:51, 30 July 2007. Recent content is available under a simple permissive license.