Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented a tokenizer for arithmetic.

  • Loading branch information...
commit a7a2977deabde759f2c2c97b6a8c7adeba815380 1 parent 7590487
Karthik Ravikanti authored
Showing with 17 additions and 0 deletions.
  1. +17 −0 haskell/evalArith.hs
17 haskell/evalArith.hs
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.