Singly-linked list/Traversal: Difference between revisions

→‎{{header|Perl 6}}: add example with custom type
(→‎{{header|Perl 6}}: add example with custom type)
Line 877:
 
=={{header|Perl 6}}==
===With <tt>Pair</tt>===
 
Built-in list processing in Perl is not specifically based on singly-linked lists,
but works at a higher abstraction level that encapsulates such implementation choices. Nonetheless, it's trivial to use the <tt>Pair</tt> type to build what is essentially a Lisp-style cons list, and in fact, the <tt>=></tt> pair constructor is right associative for precisely that reason.
Line 914 ⟶ 916:
{{out}}
<pre>Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ Ⅵ Ⅶ Ⅷ Ⅸ Ⅹ Ⅺ Ⅻ</pre>
 
===With custom type===
 
Extending <tt>class Cell</tt> from [[Singly-linked_list/Element_definition#Perl_6]]:
 
<lang perl6> method Seq {
self, *.next ...^ !*
}</lang>
 
Usage:
 
<lang perl6>my $list = (cons 10, (cons 20, (cons 30, Nil)));
 
for $list.Seq -> $cell {
say $cell.value;
}</lang>
{{out}}
<pre>10
20
30</pre>
 
=={{header|Phix}}==
Anonymous user