Singly-linked list/Element definition: Difference between revisions

Content added Content deleted
(added scheme)
Line 256: Line 256:
include Enumerable
include Enumerable
end
end

=={{header|Scheme}}==

Scheme, like other Lisp dialects, has extensive support for singly-linked lists. The element of such a list is known as a ''cons-pair'', because you use the <code>cons</code> function to construct it:
<scheme>(cons value next)</scheme>

The value and next-link parts of the pair can be deconstructed using the <code>car</code> and <code>cdr</code> functions, respectively:
<scheme>(car my-list) ; returns the first element of the list
(cdr my-list) ; returns the remainder of the list</scheme>

Each of these parts are mutable and can be set using the <code>set-car!</code> and <code>set-cdr!</code> functions, respectively:
<scheme>(set-car! my-list new-elem)
(set-cdr! my-list new-next)</scheme>