Reputation: 3434
I'm new to OCaml, and trying to write some basic functions.
I found the find_new
function(this function is basically finding a list of elements that only appear in the first list) doesn't work if I use curry2
to define it.
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]);;
if I use the second definition of find_new
(one that commented out), the error info was:
Error: This expression has type int list
but an expression was expected of type int
So I'm wondering what's wrong with my code?
Upvotes: 1
Views: 136
Reputation: 66818
This looks like the value restriction. Try defining with eta expansion
let find_new pair = curry2 find_new_ pair
See the OCaml FAQ A function obtained through partial application is not polymorphic enough.
Upvotes: 3