2
votes

I need to create a function in sml that takes a single number and returns a list of all the numbers that are prime below it. I can do that but I dont know how to create a list so i can use to see if 1 is prime then 2 then 3 then 4 then 5 and so on.

Basicly i need a way to generate a list inside of an SML function and that list has the numbers from 2 to n.

2

2 Answers

3
votes

The List.tabulate function will populate a list for you. Here's an example, giving you the numbers [2..n]:

List.tabulate(n-1, fn x => x+2);
1
votes

I found out that we can not use external libraries for this so I actually was able to come up with a solution of my own. It does the numbers from start to up to, but not including, ending

fun createList(start:int, ending:int) = if(start = ending) then
                       []
                    else
                       start::createList(start + 1, ending);