2014-04-03 58 views
0

列出我有一個返回(字符列表)列表選項,而且我試圖獲取列表的大小的功能:列表選項OCaml中

let c = recherche m ledico in 
    match c with 
    | None -> Printf.printf "Non." 
    | Some [] -> Printf.printf "Oui." 
    | _ -> 
     let n = List.length c in 
(...) 

recherche是返回我的功能(char list) list option,它可以返回None,Some []Some [[...] ; ... ; [...]]。我如何找到這個長度?我看到this解決方案,但它沒有工作:

Error: The function applied to this argument has type 'a list -> 'a list 
This argument cannot be applied with label ~f 

我如何獲得一個列表選項的大小?

回答

3

你只需要給列表一個名字。

| Some l -> let n = List.length l in ... 
+1

Oooohhh ... duh。 –

0

要應用功能'a option,你只需要上'a類型的值工作的功能,並返回當輸入爲None的值。核心庫中有一個名爲value_map的函數可以實現這一點。基本實現非常簡單:

let value_map x default f = 
    match x with 
    | None -> default 
    | Some sx -> f sx 

在你的情況,你需要選擇一個默認值。您要應用的功能是List.length