Last active
August 29, 2015 13:56
-
-
Save afternoon/9240935 to your computer and use it in GitHub Desktop.
Task 3 from West London Hack Night Feb 26 2014
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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