[erlang-questions] What does it mean when an evaluated expression is a list that contains the pipe character?
Ilya Shcherbak
tthread@REDACTED
Tue Feb 24 18:11:54 CET 2015
For better understanding Erlang lists will be better address to Lisp lists.
Actually there r no lists at all. There are only cells linked together.
It'll be good to take parallel line with Lisp:
cons-cell consist left and right value:
(1 . 2) - 1 is left value, 2 - right one
> (cons 1 2)
<---- (1 . 2)
left value named 'car' and right - 'cdr'.
'cdr' value can be a link to another cons-cell.
for construct list in Lisp u should bring all cells together via 'cdr'
links.
the tail 'cdr' element linked with 'nil'.
it looks like this:
>(cons 1 (cons 2 (cons 3 nil)))
<---- (1, 2, 3)
the same thing in Erlang:
there is a cons-cell:
> [1 | 2].
<---- [1 | 2].
u can compose Erlang list in Lisp style:
> [1 | [2 | [3 | []]]].
<---- [1,2,3].
Erlang list - is a sequence of linked cons-cells also.
tail cell should point to []. (like 'nil' in Lisp).
[1, 2 | 3] - incorrect Erlang list cuz it contains cell which 'cdr' element
isn't link.
[1, 2 | [3 | []]] or [1, 2 | [3]] - correct Erlang list cuz each 'cdr'
element in each cell shows to another cell.
' | ' operator doesn't applied to lists. it works with "cells" and values.
also u can compare list with linked-list in C but it's incorrect cuz Erlang
list can consist elements with different types.
2015-02-24 21:37 GMT+06:00 Cole Fichter <cole.fichter@REDACTED>:
> I came across the example:
>
> > [1,2|3].
> [1,2|3]
>
> I understand that the pipe character, |, can be used to append an item to
> the head of the list or to pop an item off the list in a pattern match.
>
> But what exactly is happening in the example above? Why does the evaluated
> expression still contain the pipe?
>
> My best guess is that the final expression results in a match
> specification that could be used in a pattern match. However, that seems
> strange too because if so, we'd be popping two items off the list, which
> should be illegal.
>
> Can someone shed some light?
>
> Thanks!
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150225/5d222732/attachment.htm>
More information about the erlang-questions
mailing list