2014-04-03 59 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