How can I filter out non-prime numbers in a list using Standard ML?

In summary, a function was created to find all prime numbers in a list of integers by recursively removing elements that are divisible by the first prime number and adding the remaining elements to a result list.
  • #1
azrarillian
1
0
Hi, I'm using SML and I'm trying to make a program/function that finds all the prime numbers in an Int list of numbers.

what I'm trying to do is make a function that removes any (and all) elements x of the int list where x mod p = 0, and where p is the first prime number (2).

then i want to make a recursion so that it does the same for the next element after p, which should be a prime number.

the only problem i have is that i don't know how to filter or delete the elements x in the list. I've tried to use the function 'filter' but I can't figure out how to take modulo of the tail of the list (or rather of the elements in the tail) and the prime number.

please help...

also, I know that there are other ways to find primenumbers, and though this is the way i want to use (for now) any and all help, otherwise, is welcome.
 
Last edited:
Technology news on Phys.org
  • #2
Here is one possible solution:fun primeList [] = [] | primeList (x::xs) = let val p = 2 in if x mod p = 0 then primeList xs else x :: primeList xs end;This function takes in a list of integers and recursively checks if the head of the list is divisible by the first prime number (p = 2). If it is, then it is removed and the function calls itself on the rest of the list. If it isn't, then it is added to the result list and the function calls itself on the rest of the list. This process continues until the list is empty, at which point the result list is returned.
 

1. What is Standard ML?

Standard ML is a functional programming language commonly used in the field of computer science and mathematics. It was developed in the 1980s and is known for its strong type system and ability to support modular programming.

2. What is a filter in Standard ML?

In Standard ML, a filter is a higher-order function that takes in a list and a predicate function as parameters, and returns a new list containing only the elements that satisfy the predicate. It is commonly used for data filtering and manipulation.

3. How do I use the filter function in Standard ML?

To use the filter function in Standard ML, first define a predicate function that takes in an element as a parameter and returns a boolean value. Then, pass the predicate function and a list to the filter function as parameters, and it will return a new list containing only the elements that satisfy the predicate.

4. What is mod in Standard ML?

Mod is a built-in function in Standard ML that calculates the remainder of a division operation. It takes in two integer values as parameters and returns the remainder as an integer. It is commonly used in mathematical calculations and algorithms.

5. How is mod used in Standard ML?

To use mod in Standard ML, simply call the function and pass in two integer values as parameters. The function will calculate the remainder of the division operation and return it as an integer value. It is often used in conjunction with other mathematical functions and operations.

Similar threads

Replies
15
Views
2K
  • Programming and Computer Science
3
Replies
97
Views
7K
Replies
9
Views
1K
Replies
8
Views
372
  • Programming and Computer Science
Replies
21
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
909
  • Programming and Computer Science
Replies
8
Views
876
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top