我是 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
所以我想知道我的代码有什么问题?