简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM