简体   繁体   English

质数列表

[英]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 |我不知道此时我们如何用一个参数调用 function | głowa:: ogon -> usuń ogon (List.filter (liczby_pierw głowa) listx), since that function was declared above as a function with two parameters. głowa:: ogon -> usuń ogon (List.filter (liczby_pierw głowa) listx),因为上面将 function 声明为带有两个参数的 function。

I would like someone to explain to me how this works, why we could use a function call with one argument.我希望有人向我解释这是如何工作的,为什么我们可以使用带有一个参数的 function 调用。

This is called partial function application .这称为部分 function 应用程序 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.当你有一个带有多个参数的 function 时,你可以只用前几个参数调用它,结果将是一个带有其余参数的 function。

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 ).调用liczby_pierw głowa int -> bool This is the type of function that you need to pass to filter and so you can do that without explicit fun .这是您需要传递给filter的 function 的类型,因此您可以在没有显式fun的情况下执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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