我是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 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
所以我想知道我的代码有什么问题?
答案 0 :(得分:3)
这看起来像价值限制。尝试使用eta扩展进行定义
let find_new pair = curry2 find_new_ pair
请参阅OCaml常见问题A function obtained through partial application is not polymorphic enough。