Convert code from Java to ELM

hello everybody,
I write this code in Java and i want to convert it to Elm, can you help me please ?

code 
	 public static long getPolishSum(String[] input) {
	        Stack<String> digitsStack = new Stack<String>();
	        for (int i =input.length-1; i > -1; i--) {
	            if (input[i].equals("+") ||
	                input[i].equals("-") ||
	                input[i].equals("*") ||
	                input[i].equals("/") ) {
	                long firstValue = Long.valueOf(digitsStack.pop());
	                long secondValue = Long.valueOf(digitsStack.pop());
	                long temp = 0;
	                if (input[i].equals("+")) temp = firstValue + secondValue;
	                else if (input[i].equals("-")) temp = firstValue - secondValue;
	                else if (input[i].equals("*")) temp = firstValue * secondValue;
	                else if (input[i].equals("/")) temp = firstValue / secondValue;
	                digitsStack.push(String.valueOf(temp));
	            }
	            else digitsStack.push(input[i]);
	        }
	        return Long.valueOf(digitsStack.pop());
	    }
	}

Hello Zeck,

can you provide people with more details?

  • What is your algorithm trying to accomplish?
  • What have you already tried out?

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.

1 Like

The algorithme Just Calculate expression With polish notation, I code it in java and I can also code it With javascript but I strugle With ELM syntax

A few suggestions:

  • Since everything in elm is immutable, you’ll need to rewrite this as a recursive algorithm instead of an iterative one.
  • Elm’s List works can function as a stack.
  • Since elm doesn’t have exceptions, you will probably want to return a Maybe or a Result in case the input is not properly formed.
2 Likes

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:

link to ellie: https://ellie-app.com/dT34KGTsHPsa1

(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.

thanks for the awesome information.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.