简体   繁体   中英

What is the difference between ('a * 'a) list and 'a * 'a list in ocaml?

I am trying to delcare a list of type string * string list (whose type is required in other functions), and I keep getting type mismatch errors. If I were to define a list as let a = [("a", "b")] , I get an error when I pass it into a function that expects type string * string list :

Error: This expression has type (string * string) list
       but an expression was expected of type string * string list

The same error occurs when a declare it as let a = ["a", "b"] . Why is this happening?

You are misreading the type: type application binds tighter than product, thus

string * string list

means

string * (string list)

For example:

let x: string * string list = "a", ["b"]

compared to

let y : (string * string) list = ["a", "b"; "c", "d"]

An ('a * 'a) list is a list of ('a * 'a) tuples, while a 'a * 'a list is a tuple of an 'a with a list of 'a .

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