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
List 运算符和列表f#_List_F# - Fatal编程技术网

List 运算符和列表f#

List 运算符和列表f#,list,f#,List,F#,我制定了以下代码: let rec foo1 z = function | [] -> [] | x::xs when x = z -> x::(foo1 x xs) | x::xs -> foo1 z xs 但是我想要这个函数的一个更复杂的版本,它的第一个参数是int->int->bool。此函数将针对列表中的所有元素测试integer参数,应用运算符时生成true的元素应放在返回列表中 例如: let eq x y = x = y let lt x y = x

我制定了以下代码:

let rec foo1 z = function
  | [] -> []
  | x::xs when x = z -> x::(foo1 x xs)
  | x::xs -> foo1 z xs
但是我想要这个函数的一个更复杂的版本,它的第一个参数是
int->int->bool
。此函数将针对列表中的所有元素测试integer参数,应用运算符时生成true的元素应放在返回列表中

例如:

let eq x y = x = y
let lt x y = x < y
let gt x y = x > y

foo2 eq 2 [1;2;4;2;5] => [2;2]
foo2 lt 2 [1;2;4;2;5] => [4;5]
foo2 gt 2 [1;2;4;2;5] => [1]
让等式x y=x=y
设lt x y=xy
foo2等式2[1;2;4;2;5]=>[2;2]
foo2 lt 2[1;2;4;2;5]=>[4;5]
foo2 gt 2[1;2;4;2;5]=>[1]
///将函数作为参数传递
设rec filterByVal fn z=函数
| [] -> [] 
|当fn z x->x时x::xs:(filterByVal fn z xs)
|_ux::xs->filterByVal fn z xs
///使用匿名函数
filterByVal(funzx->z=x)2[1;2;4;2;5]/[2;2]
filterByVal(funzx->zz>x)2[1;2;4;2;5]/[1]
///或者使用符号中缀函数
过滤器Y(=)2[1;2;4;2;5]
filterByVal()2[1;2;4;2;5]
///将函数作为参数传递
设rec filterByVal fn z=函数
| [] -> [] 
|当fn z x->x时x::xs:(filterByVal fn z xs)
|_ux::xs->filterByVal fn z xs
///使用匿名函数
filterByVal(funzx->z=x)2[1;2;4;2;5]/[2;2]
filterByVal(funzx->zz>x)2[1;2;4;2;5]/[1]
///或者使用符号中缀函数
过滤器Y(=)2[1;2;4;2;5]
filterByVal()2[1;2;4;2;5]

我不知道,你知道有没有办法做同样的事情,但不定义eq、lt和gt函数?这只能用整数来做,还是可以做其他事情?我不知道,你知道有没有办法做同样的事情,但不定义eq,lt和gt函数?这只能用整数来完成,还是可以是其他任何函数?
/// Pass a function as an argument 
let rec filterByVal fn z = function 
    | [] -> [] 
    | x::xs when fn z x -> x::(filterByVal fn z xs) 
    | _::xs -> filterByVal fn z xs

/// Use anonymous functions
filterByVal (fun z x -> z = x) 2 [1;2;4;2;5] // [2; 2]
filterByVal (fun z x -> z < x) 2 [1;2;4;2;5] // [4; 5]
filterByVal (fun z x -> z > x) 2 [1;2;4;2;5] // [1]

/// Or use symbolic infix functions
filterByVal (=) 2 [1;2;4;2;5]
filterByVal (<) 2 [1;2;4;2;5]
filterByVal (>) 2 [1;2;4;2;5]