Skip to content

Instantly share code, notes, and snippets.

@iliyan-trifonov
Created September 2, 2016 13:28
Show Gist options
  • Save iliyan-trifonov/5bf00deaa665d416c6ce66db5cf73c32 to your computer and use it in GitHub Desktop.
Save iliyan-trifonov/5bf00deaa665d416c6ce66db5cf73c32 to your computer and use it in GitHub Desktop.
Sum 10 numbers in a JS for loop -> converted to Elm with recursion
-- original JS code
--var result = 0;
--for ( var i = 0; i <= 10; i++) {
-- result += i;
--}
-- we will use Html.text to show the result
import Html exposing (text)
-- let's call the function `sumMe`
-- it takes an integer as its only param and returns another integer - the sum
sumMe : Int -> Int
sumMe num =
-- stop the recursion when the number is decreased to zero
if num == 0 then
0
else
-- continue the recursion while addin the rest of the numbers
num + sumMe (num-1)
main =
-- starting with the last number 10 so we don't need to give another param
text ( toString (sumMe 10) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment