Created
September 2, 2016 13:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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