Skip to content

Instantly share code, notes, and snippets.

@sordina
Created November 13, 2013 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sordina/7458446 to your computer and use it in GitHub Desktop.
Save sordina/7458446 to your computer and use it in GitHub Desktop.
module Algebra where
import Data.List.Split
import Data.List
import Data.Char
import Data.Maybe
import Safe
interpret :: String -> Int -> Int -> Int
interpret s x y = addFun $ substitute "x" (show x) $ substitute "y" (show y) $ s
substitute :: String -> String -> String -> String
substitute var val s = intercalate val $ splitOn var s
addFun :: String -> Int
addFun = sum . map timesFun . splitOn "+"
timesFun :: String -> Int
timesFun s = if elem '*' s then product $ map literalFun $ splitOn "*" s
else (literalFun s)
literalFun :: String -> Int
literalFun s = fromMaybe 42 $ readMay (filter isDigit s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment