Ocaml 什么';“我的”怎么了;“货币2”;实施
我是OCaml新手,正在尝试编写一些基本函数 我发现Ocaml 什么';“我的”怎么了;“货币2”;实施,ocaml,Ocaml,我是OCaml新手,正在尝试编写一些基本函数 我发现find_new函数(该函数基本上是查找仅出现在第一个列表中的元素列表)在我使用curry2定义它时不起作用 let curry2 f (x,y) = f x y let flip f x y = f y x let compose f g x = g (f x) let rec elem v xs = match xs with [] -> false | (h::t) -> if v = h
find_new
函数(该函数基本上是查找仅出现在第一个列表中的元素列表)在我使用curry2
定义它时不起作用
let curry2 f (x,y) = f x y
let flip f x y = f y x
let compose f g x = g (f x)
let rec elem v xs = match xs with
[] -> false
| (h::t) -> if v = h then true else elem v t
let rec filter fb l = match l with
[] -> []
| (h::t) -> if fb h
then h::(filter fb t)
else (filter fb t)
let x = [5;6;7;3] ;;
let y = [5;6;7;5] ;;
let z = [7;5;6;5] ;;
let a = [3;5;8;9] ;;
let find_new_ xs ys = filter (compose ((flip elem) ys) not) xs ;;
let find_new (xs,ys) = find_new_ xs ys;; (* works *)
(* let find_new = curry2 find_new_;; *) (* doesn't work *)
find_new (x,[3]);;
find_new (x,[3;5]);;
find_new (x,[3;6]);;
find_new ([x;y;z],[y]);;
find_new ([x;y;z],[y;x]);;
如果我使用find_new
的第二个定义(注释掉的定义),错误信息是:
Error: This expression has type int list
but an expression was expected of type int
所以我想知道我的代码出了什么问题?这看起来像是值限制。尝试使用eta扩展定义
let find_new pair = curry2 find_new_ pair
请参阅OCaml常见问题。值限制与Haskell中的“单态限制”是否相同?不,它是不同的限制。Haskell不需要值限制,因为它没有“副作用”。