Anonymous user
Singly-linked list/Element definition: Difference between revisions
Singly-linked list/Element definition (view source)
Revision as of 04:53, 18 August 2008
, 15 years agoadded scheme
(added scheme) |
|||
Line 256:
include Enumerable
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>
|