Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#是否给出函数参数-什么';有什么区别_F# - Fatal编程技术网

F#是否给出函数参数-什么';有什么区别

F#是否给出函数参数-什么';有什么区别,f#,F#,我有两个函数用来做多项式的基本运算。最初,我定义函数时没有显式地提供参数,但当涉及到在另一个函数中使用其中一些函数时,事情就开始分崩离析,直到我将它们更改为使用参数 代码更能解释它: //Multiplying a polynomial with a constant let rec mulC = function | (_,[]) -> [] | (x,y::tail) when x > 0 -> x * y::mulC(x,tai

我有两个函数用来做多项式的基本运算。最初,我定义函数时没有显式地提供参数,但当涉及到在另一个函数中使用其中一些函数时,事情就开始分崩离析,直到我将它们更改为使用参数

代码更能解释它:

//Multiplying a polynomial with a constant
let rec mulC = function
| (_,[])                    -> []
| (x,y::tail) when x > 0    -> x * y::mulC(x,tail)

//Adding two polynomials
let rec addE = function
| ([],[])                   -> []
| ([], y::ytail)            -> y::ytail
| (x::xtail,[])             -> x::xtail
| (x::xtail, y::ytail)      -> (x + y) :: addE(xtail,ytail)

//Multiplying a polynomial by x
let mulX = function
| []                    -> []
| xs                    -> 0::xs

//Multiplying two polynomials
let rec mul = function
| []                    -> []
| x::tail               -> addE (mulC x qs)
                                (mulX(mul qs tail))
现在,这给出了一个错误,该值不是函数,不能应用于函数mul最后的加法

但是,如果我将函数定义更改为此,它将起作用:

let rec mulC x ys =
match (x,ys) with
| (_,[])                    -> []
| (x,y::ys) when x > 0    -> x * y::mulC x ys

let rec addE xs ys =
match (xs, ys) with
| ([],[])                   -> []
| ([], y::ys)            -> y::ys
| (x::xs,[])             -> x::xs
| (x::xs, y::ys)      -> (x + y) :: addE xs ys

let mulX xs = 
match xs with
| []                    -> []
| xs                    -> 0::xs

let rec mul qs = function
| []                    -> []
| x::tail               -> addE (mulC x qs)
                                (mulX(mul qs tail))

给出了什么?

关键字
函数
创建了一个函数,该函数接受一个参数,并直接进行模式匹配。这三个函数都是等价的,其类型签名为
(int*int)->int
。只有一个2元组参数:

let add = function (a, b) -> a + b
let add' = fun x -> match x with (a, b) -> a + b
let add'' x = match x with (a, b) -> a + b
而此函数的类型签名为
int->int->int
。有两个参数:

let add a b = a + b

但是为什么会出现错误
此值不是函数,无法应用。
?因为
addE
接受一个参数并返回一个
列表
。您应用了一个参数,因此类型系统推断您有一个列表,然后尝试对该列表应用另一个参数,但列表不是函数。您可以通过以下代码获得相同的错误:

[] 1

第一个定义期望一个2元组作为参数,而不是两个单独的参数。