Browse files

Implemented a tokenizer for arithmetic.

  • Loading branch information...
1 parent 7590487 commit a7a2977deabde759f2c2c97b6a8c7adeba815380 @plumenator committed May 22, 2012
Showing with 17 additions and 0 deletions.
  1. +17 −0 haskell/evalArith.hs
View
17 haskell/evalArith.hs
@@ -0,0 +1,17 @@
+module Main where
+import Data.Char(isDigit)
+
+tokenize :: String -> [String]
+tokenize exp = tok exp ""
+ where tok "" "" = []
+ tok "" ys = [ys]
+ tok (x:xs) "" = tok xs (x:[])
+ tok (x:xs) curr@(y:ys) = if (isDigit x `xor` isDigit y) then curr:(tok xs [x])
+ else if (isOperand x) then curr:[x]:(tok xs "")
+ else (tok xs (curr ++ [x]))
+ xor = (/=)
+ isOperand '*' = True
+ isOperand '+' = True
+ isOperand '-' = True
+ isOperand '/' = True
+ isOperand _ = False

0 comments on commit a7a2977

Please sign in to comment.