aboutsummaryrefslogtreecommitdiffstats
path: root/examples/linked-list.scm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-07-29 21:18:46 -0400
committerGravatar Peter McGoron 2024-07-29 21:18:46 -0400
commitc18bee846d62fc47473a2a80f5ef005841a2c73a (patch)
tree17c6639ba9400d51b1fd819b8502354d7dfbd29c /examples/linked-list.scm
parentdefine-namespace and SRFI-1 (diff)
aatree and linked-listHEADmaster
Diffstat (limited to '')
-rw-r--r--examples/linked-list.scm27
1 files changed, 27 insertions, 0 deletions
diff --git a/examples/linked-list.scm b/examples/linked-list.scm
new file mode 100644
index 0000000..821d0b6
--- /dev/null
+++ b/examples/linked-list.scm
@@ -0,0 +1,27 @@
+(define-namespace linked-list
+ (define-record-type linked-list
+ (linked-list head tail)
+ linked-list?
+ (head get-head set-head!)
+ (tail get-tail set-tail!))
+ (define (new) (linked-list '() '()))
+ (define (empty? lst) (null? (get-head lst)))
+ (define (linked-list->list lst) (get-head lst))
+ (define (push-first lst val)
+ (let ((container (list val)))
+ (set-head! lst container)
+ (set-tail! list container)))
+ (define (push-head lst val)
+ (if (empty? lst)
+ (push-first lst val)
+ (set-head! lst (cons val (get-head lst)))))
+ (define (push-tail lst val)
+ (if (empty? lst)
+ (push-first lst val)
+ (let ((container (list val)))
+ (set-cdr! (get-tail lst) container)
+ (set-tail! lst container))))
+ (export new empty?
+ push-head push-tail
+ linked-list->list))
+