DEV Community

loading...

Discussion on: Daily Challenge #4 - Checkbook Balancing

Collapse
cloudyhug profile image
cloudyhug

Haskell

import Data.Char

processCheckbook :: String -> String
processCheckbook checkbook =
  unlines $ prettyBalance : (reverse prettyEntries) ++ [prettyTotal, prettyAverage]
  where
    (originalBalance : entries) = lines $ filter (\x -> or [isAlphaNum x, isSpace x, x == '.']) checkbook
    processEntry (prettyEntries, total, balance) entry =
      let expense = read $ words entry !! 2
          newBalance = balance - expense
      in ((entry ++ " Balance " ++ show newBalance) : prettyEntries, total + expense, newBalance) 
    (prettyEntries, totalExpense, _) = foldl processEntry ([], 0, read originalBalance) entries
    prettyBalance = "Original_Balance: " ++ originalBalance
    prettyTotal = "Total expense " ++ show totalExpense
    prettyAverage = "Average expense " ++ show (totalExpense / (fromIntegral $ length entries))