Home > Software design >  what does the ocaml type string list -> ( string * string * string) list mean
what does the ocaml type string list -> ( string * string * string) list mean

Time:03-26

could someone explain to me the type

string list -> ( string * string * string) list 

specifically the return type because i don't really understand

CodePudding user response:

  • -> indicates a function type from one type to another.
  • string list is the type of a list of strings.
  • string * string * string is the type of a tuple of three strings.
  • (string * string * string) list is the type of a list of those tuples.

Therefore string list -> ( string * string * string) list is the type of a function which maps from a list of strings to a list of tuples of three strings.

CodePudding user response:

To give an example (it is nonsensical, just to give some intuition about types):

let f lst = List.map (fun str -> (String.trim str, "random", "stuff")) lst

It might be a good exercise to find out, why I could not write something like:

let f lst = List.map (fun _ -> ("string", "random", "stuff")) lst

(hint: because the list would not have to be a list of strings, so OCaml would call it 'a list instead of string list)

  • Related