Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whet does it means to append a integer to a list in erlang?

Tags:

erlang

The normal way to do a list appending is via:

10> [1,2,3] ++ [4].
[1,2,3,4]

But after I convert it to the following way, I actually don't get the point what does the result means here:

11> [1,2, 3] ++ 4.  
[1,2,3|4]

Could anyone give me a explanation? Many thanks.

like image 241
Judking Avatar asked Dec 07 '25 06:12

Judking


1 Answers

The Erlang lists are described in Getting Started with Erlang User's Guide in chapter Sequential Programming and subchapter Lists. The operator | separates a head of the list from a tail. The proper list ends with the empty list. The syntax with , is just syntactic sugar.

1> [1|[2|[3|[]]]].
[1,2,3]

It is like CONS function in Lisp. The list is called improper list if doesn't end with the empty list.

2> [1|[2|[3|4]]]. 
[1,2,3|4]

You made the improper list by appending number instead of a proper list. ([4] is proper list [4|[]].) See my answer to how is a list constructured by the erlang vm? for more details how it works internally in BEAM VM.

like image 130
Hynek -Pichi- Vychodil Avatar answered Dec 09 '25 20:12

Hynek -Pichi- Vychodil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!