# Convert code from Java to ELM

**URL:** <https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604>\
**Category:** Learn\
**Created:** [July 31, 2021, 12:49pm UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604 "2021-07-31T12:49:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [July 31, 2021, 12:49pm UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/1 "2021-07-31T12:49:20Z")

</div>

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

```auto
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());
	    }
	}

```

---

<div class="post-metadata">

**Author:** ![IloSophiep](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ilosophiep/32/3489_2.png) [@IloSophiep](https://discourse.elm-lang.org/u/IloSophiep)\
**Post date:** [July 31, 2021, 1:10pm UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/2 "2021-07-31T13:10:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [July 31, 2021, 2:11pm UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/3 "2021-07-31T14:11:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dta/32/1917_2.png) [@dta](https://discourse.elm-lang.org/u/dta)\
**Post date:** [August 1, 2021, 12:36am UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/4 "2021-08-01T00:36:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grigorious](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/grigorious/32/3549_2.png) [@grigorious](https://discourse.elm-lang.org/u/grigorious)\
**Post date:** [August 1, 2021, 11:59am UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/5 "2021-08-01T11:59:57Z")

</div>

Hi, assuming you are trying to parse a polish notation expression like this one:

```auto
+ 1 2 3 4 5 6

// what about
* (- 5 6) 7
// or
* 4 5 + 4 5
// ...

```

I would suggest you make a [test file](https://package.elm-lang.org/packages/elm-explorations/test/latest/) 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](https://ellie-app.com/dT34KGTsHPsa1)

(if this is for an assignment this will not get you best marks or maybe even passing marks)

```elm
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"

```

---

<div class="post-metadata">

**Author:** ![Zack\_slm](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/zack_slm/32/4166_2.png) [@Zack\_slm](https://discourse.elm-lang.org/u/Zack_slm)\
**Post date:** [August 1, 2021, 1:34pm UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/6 "2021-08-01T13:34:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jackyjoy123](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jackyjoy123](https://discourse.elm-lang.org/u/jackyjoy123)\
**Post date:** [August 7, 2021, 11:27am UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/7 "2021-08-07T11:27:18Z")

</div>

thanks for the awesome information.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [August 17, 2021, 11:27am UTC](https://discourse.elm-lang.org/t/convert-code-from-java-to-elm/7604/8 "2021-08-17T11:27:32Z")

</div>

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