2014-02-06 67 views
0

我真的不明白是怎麼回事。我有以下代碼:在corebuild下編譯時出現奇怪的錯誤

The expression has type 'a list -> `a list list but an expression was expected of type 'b list

是否有事可做:

let rec interleave n l = 
match l with 
    [] -> [[n]] 
    | head::tail -> (n::l)::(List.map (~f:fun y -> head::y) (interleave n tail)) 
in let rec aux l = 
match l with 
    [] -> [l] 
    | head::tail -> List.concat (List.map (interleave head) (aux tail)) 

ocaml的它編譯和預期,但下corebuild它給了我下面的錯誤工作編譯再次標籤(正如你從~f:fun y -> ...看到的那樣,它已經讓我感到厭煩了)?如果有,我應該使用哪種標籤以及在哪裏?

回答

2

您需要重讀有關標記參數的手冊的一部分。

let rec interleave n l = 
match l with 
    | [] -> [[n]] 
    | head::tail -> (n::l)::(List.map ~f:(fun y -> head::y) (interleave n tail)) 
and aux l = 
match l with 
    | [] -> [l] 
    | head::tail -> List.concat (List.map ~f:(interleave head) (aux tail));; 

N.B.標籤參數的正確語法是~label:expression

N.B.核心List.map函數的類型爲'a list -> f:('a -> 'b) -> 'b list,如果您忘記將標籤添加到您的函數f它將嘗試將第二個參數與函數統一起來。這就是爲什麼你有這麼奇怪的錯誤信息。

+0

我認爲函數'〜f'只添加到「lambda」函數,但現在它是有道理的 – Gasim