1

私は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(コメントアウトされた)の2番目の定義を使用すると、エラー情報は次のようになりました。

Error: This expression has type int list
       but an expression was expected of type int

だから、私のコードの何が問題なのだろうか?

4

1 に答える 1

3

これは、値の制限のように見えます。eta展開で定義してみる

let find_new pair = curry2 find_new_ pair

OCaml FAQ を参照してください。部分的な適用によって得られた関数は、十分に多態的ではありません

于 2013-10-10T08:01:39.233 に答える