czitak
czitak

Reputation: 31

List of prime numbers

let liczby_pierw n x =
x = n || x % n <> 0

    let rec usuń listn listx =
        match listn with
        | głowa :: ogon ->  usuń ogon (List.filter (liczby_pierw głowa) listx)
        | [] -> listx
    
    let liczby_pierw1 n =
        let max = int (sqrt(float n)) 
        usuń [ 2 .. max ] [ 2.. n ]
    printfn "Liczby pierwsze od 2 do %d:\n %A" 100 (liczby_pierw1 100)

I have no idea how we can call a function with one parameter at this point | głowa :: ogon -> usuń ogon (List.filter (liczby_pierw głowa) listx) , since that function was declared above as a function with two parameters.

I would like someone to explain to me how this works, why we could use a function call with one argument.

Upvotes: 0

Views: 61

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243096

This is called partial function application. When you have a function with multiple parameters, you can call it with just first few and the result will be a function that takes the remaining parameters.

When you write:

usuń ogon (List.filter (liczby_pierw głowa) listx)

It is actually the same as writing:

usuń ogon (List.filter (fun x -> liczby_pierw głowa x) listx)

The result of calling liczby_pierw głowa is a function that expects one more parameter (a function of type int -> bool). This is the type of function that you need to pass to filter and so you can do that without explicit fun.

Upvotes: 1

Related Questions