zoran119
zoran119

Reputation: 11307

List type signatures

This makes sense:

test :: [[Int]]
test = [[]]

main = print test

But why does this compile (and run)?

test :: [[Int]]
test = []

main = print test

Upvotes: 3

Views: 278

Answers (2)

dkasak
dkasak

Reputation: 2703

The type of [] is polymorphic, i.e. forall t. [t], meaning it represents an empty list of any type t. Since test is of type [[Int]], we can unify t ~ [Int] and therefore [] is an empty list of type [[Int]] also.

Upvotes: 4

[[Int]] is list of lists (of Int's) and

  • [[]] - list with one empty list inside
  • [] - empty list

Upvotes: 9

Related Questions