OCaml,为什么这种类型是'a list list 而不是'a list
OCaml, why is this type 'a list list and not 'a list
let rec morse2 c s1 s2 = match s1 with
| [] -> []
| g::r when (caps c = g) -> List.hd(s2)
| g::r -> morse2 c r (List.tl(s2));;
val morse2 : char -> char list -> 'a list list -> 'a list = <fun>
嗨,
我的函数得到一个字符和两个列表,第一个是字符列表,第二个应该是字符串列表,但由于某种原因它只接受字符串列表列表。该函数的作用基本上是检查第一个列表中的字符是否与作为输入给出的字符匹配,如果匹配 returns 第二个列表中相同位置的元素应该是字符串而不是字符串列表。
caps c
函数仅将字符转换为大写字母。
行| [] -> []
告诉编译器函数return是一个列表,然后| g::r when (caps c = g) -> List.hd(s2)
行告诉编译器函数可以returnList.hd(s2)
.因此 List.hd(s2)
必须是列表并且 s2
必须是列表的列表。
let rec morse2 c s1 s2 = match s1 with
| [] -> []
| g::r when (caps c = g) -> List.hd(s2)
| g::r -> morse2 c r (List.tl(s2));;
val morse2 : char -> char list -> 'a list list -> 'a list = <fun>
嗨,
我的函数得到一个字符和两个列表,第一个是字符列表,第二个应该是字符串列表,但由于某种原因它只接受字符串列表列表。该函数的作用基本上是检查第一个列表中的字符是否与作为输入给出的字符匹配,如果匹配 returns 第二个列表中相同位置的元素应该是字符串而不是字符串列表。
caps c
函数仅将字符转换为大写字母。
行| [] -> []
告诉编译器函数return是一个列表,然后| g::r when (caps c = g) -> List.hd(s2)
行告诉编译器函数可以returnList.hd(s2)
.因此 List.hd(s2)
必须是列表并且 s2
必须是列表的列表。