diff options
| author | 2024-07-29 21:18:46 -0400 | |
|---|---|---|
| committer | 2024-07-29 21:18:46 -0400 | |
| commit | c18bee846d62fc47473a2a80f5ef005841a2c73a (patch) | |
| tree | 17c6639ba9400d51b1fd819b8502354d7dfbd29c /examples/linked-list.scm | |
| parent | define-namespace and SRFI-1 (diff) | |
Diffstat (limited to '')
| -rw-r--r-- | examples/linked-list.scm | 27 |
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)) + |
