Hello i try to use a regex first time in ELM, could you please help me to complete this statement
getPolishSumHelper : List String -> List String -> String
getPolishSumHelper inputs digitsStack =
let
_ = Debug.log "inputs" inputs
_ = Debug.log "digitsStack" digitsStack
in
case ( inputs, digitsStack ) of
( [], topOfStack :: _ ) -> if topOfStack == Regex.fromString"([^0-9]?)" then "ERROR" else topOfStack
( "+" :: rest, first :: second :: stack ) ->
calcNext (+) rest first second stack
( "-" :: rest, first :: second :: stack ) ->
calcNext (-) rest first second stack
( "*" :: rest, first :: second :: stack ) ->
calcNext (*) rest first second stack
( "/" :: rest, first :: second :: stack ) ->
if second == "0" then "0" else calcNext (/) rest first second stack
( number :: rest, stack ) ->
getPolishSumHelper rest <| number :: stack
_ ->
"ERROR"
You will see that the function fromString accepts a String and returns a Maybe Regex
fromString : String -> Maybe Regex
There is also an example in the docs - you should read this carefully. (The docs are of high quality and you should read them first when you get stuck with a function or module)
Also look at the containsdocumentatin - it might be what you want ?!
In your second to last case branch you have the variable number. You can use the function String.toInt number to check if it’s a number. To see if it is a single letter you can use String.uncons number to see if it is a single Char, by doing something like
case String.uncons number of
Just ( firstChar, [] ) -> -- just a single Char
Char.isAlpha firstChar -- returns a Bool of whether or not the Char is in-zA-Z
_ -> -- either empty or multiple Char