elseif statement in Standard ML?

jfisk picture jfisk · Jan 29, 2012 · Viewed 21.9k times · Source

Im doing a homework problem to make a function sumOdd to computer the sum of the first n odd integers, but i cant seem to find any sort of elseif type statement to do so. What im trying to do is below but of course doesnt work:

fun sumOdd n = if n=0 then 0 elseif (n mod 2)=0 then sumOdd(n-1) elseif n + sumOdd(n-1);

Answer

pad picture pad · Jan 29, 2012

Your function didn't compile because elseif is not a keyword in SML. Changing the last elseif to else and other elseif to else if should fix the error.

Furthermore, the function is more readable in the below format:

fun sumOdd n = if n = 0 then 0 
               else if n mod 2 = 0 then sumOdd(n-1) 
               else n + sumOdd(n-1)