Maybe by answering those questions, people can lead you towards your goal - not just give you a finished answer. Also explaining the algorithm might make you understand how to model it in Elm yourself! Sometimes “talking out loud” is all that’s needed to wrap your brain around something.
Right now it feels like you simply ask other people to do the work for you. Which of course is fine, if people feel like doing so! But honestly, it feels like low effort from you.
Hi, assuming you are trying to parse a polish notation expression like this one:
+ 1 2 3 4 5 6
// what about
* (- 5 6) 7
// or
* 4 5 + 4 5
// ...
I would suggest you make a test file with test inputs - it will help you refine your logic. Start with the most trivial and simple like + 1 2 and continue by adding complexity.
Your algorithm will not work, since it will only add 1 + 2 and output 3 ignoring the rest. I think what you are trying to create is a parser? How will you handle operator precedence?
Anyway if you just want the same function with the same limitations as the Java one:
(if this is for an assignment this will not get you best marks or maybe even passing marks)
getPoslishSum : List String -> Result String String
getPoslishSum inputs =
getPolishSumHelper (List.reverse inputs) []
calcNext : (Float -> Float -> Float) -> List String -> String -> String -> List String -> Result String String
calcNext func remainingInputs first second stack =
case ( String.toFloat first, String.toFloat second ) of
( Just f, Just s ) ->
getPolishSumHelper remainingInputs <| String.fromFloat (func f s) :: stack
_ ->
Err "Malformed input"
getPolishSumHelper : List String -> List String -> Result String String
getPolishSumHelper inputs digitsStack =
let
_ = Debug.log "inputs" inputs
_ = Debug.log "digitsStack" digitsStack
in
case ( inputs, digitsStack ) of
( [], a::_ ) ->
Ok a
( "+" :: 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 ) ->
calcNext (/) rest first second stack
( number :: rest, stack ) ->
getPolishSumHelper rest <| number :: stack
_ ->
Err "Malformed input"
Hello thank you for you response.
I write this algorithme just for prefix notation (operator first then number)
I write a parser String to List of String
I get the input (String) then make a list of String
input “* + 3 5 20” → [’*’,’+’, ‘3’, ‘5’, ‘20’] and then apply my algorithm, I will handle tests after that.
for the result i try to get an INT.