SML Function to make a list from 1 to n
You're 99% of the way there. With your current function, createList(1, 7)
yields [1, 2, 3, 4, 5, 6]
. You just need another function that automatically fills in one of the parameters.
fun createList(start, ending) =
if start = ending then []
else start :: createList(start + 1, ending);
fun createList'(ending) =
createList(1, ending + 1);
Try this and createList'(6)
will yield [1, 2, 3, 4, 5, 6]
.
Now, let's say you don't want both to be visible. You can "hide" one function as a local binding.
fun createList(ending) =
let
fun aux(start, ending) =
if start = ending then []
else start :: aux(start + 1, ending)
in
aux(1, ending + 1)
end;