-
-
Save jeaye/1c8ca9e6bf357175a7689b499bf8129f to your computer and use it in GitHub Desktop.
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
module Main exposing (main) | |
main : String -> Int | |
main args = | |
let | |
tokens = | |
String.words args | |
result = | |
List.foldl evaluator initialEvaluatorState tokens | |
in | |
result.value | |
type alias EvaluatorState = | |
{ operator : Operator | |
, value : Int | |
} | |
type Operator | |
= Unknown | |
| Plus | |
| Minus | |
initialEvaluatorState : EvaluatorState | |
initialEvaluatorState = | |
{ operator = Unknown | |
, value = 0 | |
} | |
evaluator : String -> EvaluatorState -> EvaluatorState | |
evaluator token state = | |
case String.toInt token of | |
Just num -> | |
case state.operator of | |
Unknown -> | |
{ state | value = num } | |
Plus -> | |
{ state | value = state.value + num } | |
Minus -> | |
{ state | value = state.value - num } | |
Nothing -> | |
case token of | |
"+" -> | |
{ state | operator = Plus } | |
"-" -> | |
{ state | operator = Minus } | |
otherwise -> | |
{ state | operator = Unknown } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment