-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d87598f
commit 3a2a8a9
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module RPN ( | ||
solve | ||
) where | ||
|
||
import Data.Char | ||
|
||
data Expr a = Plus | Minus | Prod | Const a deriving (Show) | ||
|
||
solve :: (Num a) => [Expr a] -> Maybe a | ||
solve = solve' [] | ||
|
||
solve' :: (Num a) => [a] -> [Expr a] -> Maybe a | ||
solve' acc (Const x:xs) = solve' (x:acc) xs | ||
solve' (x:y:acc) (op:xs) = solve' (val:acc) xs | ||
where val = case op of Plus -> x + y | ||
Minus -> x - y | ||
Prod -> x * y | ||
solve' [res] [] = Just res | ||
solve' _ _ = Nothing | ||
|
||
|
||
parse :: String -> [Expr Int] | ||
parse "" = [] | ||
parse (x:xs) | ||
| x == '+' = Plus : parse xs | ||
| x == '-' = Minus : parse xs | ||
| x == '*' = Prod : parse xs | ||
| x == ' ' = parse xs | ||
| x >= '0' && x <= '9' = case parseInt(x:xs) of | ||
(val, str) -> Const val : parse str | ||
| otherwise = [] | ||
|
||
parseInt :: String -> (Int, String) | ||
parseInt = parseInt' 0 | ||
|
||
parseInt' :: Int -> String -> (Int, String) | ||
parseInt' acc "" = (acc, "") | ||
parseInt' acc (x:xs) | ||
| x >= '0' && x <= '9' = parseInt' (10 * acc + curDigit) xs | ||
| otherwise = (acc, x:xs) | ||
where curDigit = Data.Char.ord(x) - Data.Char.ord('0') | ||
|
||
parse2 :: String -> [Expr Int] | ||
parse2 = map stringToExpr . words | ||
where stringToExpr "+" = Plus | ||
stringToExpr "-" = Minus | ||
stringToExpr "*" = Prod | ||
stringToExpr num = Const (read num :: Int) |