简体   繁体   中英

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. 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.

the caps c function only converts the char in a captial letter.

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) tells the compiler that the function can return List.hd(s2) . Hence List.hd(s2) must be a list and s2 must be a list of list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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