Traversal
With Pair
Pair
Built-in list processing in Raku 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 Pair
type to build what is essentially a Lisp-style cons list, and in fact, the =>
pair constructor is right associative for precisely that reason. We traverse such a list here using a 3-part loop:
Output:
It would be pretty easy to make such lists iterable as normal Raku lists, if anyone really cared to...
Well, shoot, let's just go ahead and do it.
We'll pretend the Pair
type is really a list type.
(And we show how you turn an ordinary list into a cons list using a reduction.
Note how the [=>]
reduction is also right associative, just like the base operator.)
Output:
With custom type
Extending class Cell
from Singly-linked_list/Element_definition#Raku:
Usage:
Output:
Last updated