Katianie
Katianie

Reputation: 609

list numbers 1 to n in function - SML

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.

Upvotes: 2

Views: 4158

Answers (2)

Katianie
Katianie

Reputation: 609

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);

Upvotes: 1

Emil Vikström
Emil Vikström

Reputation: 91983

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);

Upvotes: 3

Related Questions