Skip to content

Instantly share code, notes, and snippets.

@afternoon
Last active August 29, 2015 13:56
Show Gist options
  • Save afternoon/9240935 to your computer and use it in GitHub Desktop.
Save afternoon/9240935 to your computer and use it in GitHub Desktop.
Task 3 from West London Hack Night Feb 26 2014
-- DOES NOT COMPILE
import String (toInt, toList)
data Node = Op String Node Node | Value Int
listToInt l = case toInt (join "" l) of
Just x -> x
tokeniseOp hd tl tokenAccum partialAccum =
if length partialAccum > 0 then
tokenise tl (tokenAccum ++ (listToInt partialAccum) ++ hd) partialAccum
else
tokenise tl (tokenAccum ++ hd) partialAccum
tokenise l tokenAccum partialAccum = case l of
hd :: tl ->
case hd of
"+" -> tokeniseOp hd tl tokenAccum partialAccum
"-" -> tokeniseOp hd tl tokenAccum partialAccum
"*" -> tokeniseOp hd tl tokenAccum partialAccum
"/" -> tokeniseOp hd tl tokenAccum partialAccum
_ -> tokenise tl tokenAccum (partialAccum ++ hd)
[] -> tokenAccum -- handle partialAccum here
parse s = tokenise (toList s) [] []
-- test = "34+22*5"
-- assert (parse text) == ((Op "+") (Int 34) ((Op "*") (Int 22) (Int 5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment