简体   繁体   English

OCaml,为什么此类型是“列表列表”而不是“列表”

[英]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>

Hi, 嗨,

my function get a char and two lists, the first one is a list of characters and the second is supposed to be a list of strings but for some reason it only accepts a list of lists of strings. 我的函数有一个char和两个列表,第一个是字符列表,第二个应该是字符串列表,但是由于某种原因,它只接受一个字符串列表。 what the function does is basically check if the char in the first list matches the char given as input and if it does it returns the element in the same position in the second list which is supposed to be a string and not a string list. 该函数的作用基本上是检查第一个列表中的char是否匹配作为输入的给定char,如果是,则返回第二个列表中相同位置的元素,该元素应该是字符串而不是字符串列表。

the caps c function only converts the char in a captial letter. caps c函数仅将char转换为caps c字母。

The line | [] -> [] 线| [] -> [] | [] -> [] tells the compiler that the function returns a list, then the line | g::r when (caps c = g) -> List.hd(s2) | [] -> []告诉编译器该函数返回一个列表,然后| g::r when (caps c = g) -> List.hd(s2) | g::r when (caps c = g) -> List.hd(s2) tells the compiler that the function can return List.hd(s2) . | g::r when (caps c = g) -> List.hd(s2)告诉编译器该函数可以返回List.hd(s2) Hence List.hd(s2) must be a list and s2 must be a list of list. 因此List.hd(s2)必须是列表,而s2必须是列表的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM