Difference between revisions of "Programming performance/ArthurVanLeeuwen Haskell"

From HaskellWiki
Jump to navigation Jump to search
Line 1: Line 1:
  +
Didn't feel like thinking at all, so this is a trivial recursive solution.
  +
 
<haskell>
 
<haskell>
 
main = do xs <- getContents
 
main = do xs <- getContents

Revision as of 13:39, 8 March 2007

Didn't feel like thinking at all, so this is a trivial recursive solution.

main = do xs <- getContents
          let cashheld = runStrategy 10000 . reverse . getDays . filter (\l -> head l /= '#') . filter (not . null) $ lines xs
          putStrLn $ "Cash held: " ++ show cashheld

getDays :: [String] -> [(String,Double,Double,Double,Double,Integer,Double)]
getDays = map getDay

getDay :: String -> (String,Double,Double,Double,Double,Integer,Double)
getDay s = getDay' $ words s
    where getDay' [date,open,high,low,close,volume,adjustedclose] = (date,read open,read high,read low,read close,read volume,read adjustedclose)

runStrategy cash lines = runStrategy' cash lines []

runStrategy' cash lines shares = 
    case lines of
        (c1:c2:_) -> if (closing c2 - closing c1) / closing c1 < - 0.03 then
                        runStrategy' (cash * 0.9) (tail lines) ((((cash * 0.1) / closing c2), closing c2): shares)
                     else if not . null $ shares then
                             if (closing c2 - (snd . head $ shares)) / (snd . head $ shares) > 0.06 then
                                runStrategy' (cash + (fst . head $ shares) * closing c2) (tail lines) (tail shares)
                             else runStrategy' cash (tail lines) shares
                          else runStrategy' cash (tail lines) shares
        [c1] -> cash + sellOff (closing c1) shares
        [] -> cash

sellOff price shares = sum . map (* price) . map fst $ shares

closing (date,open,high,low,close,volume,adjustedclose) = close