AJed
AJed

Reputation: 578

About the | operator in erlang.

We can make a nested list in erlang by writing something like this:

 NL = [[2,3], [1]]. 
 [[2,3],[1]]

But assume we wrote it like this instead:

 OL = [[2,3]|1].
 [[2,3]|1]

Is OL still a list? Can someone please elaborate more what OL is?

Upvotes: 4

Views: 162

Answers (2)

rvirding
rvirding

Reputation: 20916

More information about | and building list is given in Functional Programming: what is an "improper list"? .

Upvotes: 3

johlo
johlo

Reputation: 5500

This is called an improper list and should typically not be used. I think most library functions expects proper lists (e.g. length([1|2]) throws bad argument exception). Pattern matching with improper lists works though.

For some use cases, see Practical use of improper lists in Erlang (perhaps all functional languages)

Upvotes: 5

Related Questions