Aidan M
Aidan M

Reputation: 47

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?

Upvotes: 3

Views: 174

Answers (2)

octachron
octachron

Reputation: 18902

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"]

Upvotes: 2

CH.
CH.

Reputation: 606

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.

Upvotes: 1

Related Questions