define-namespace/examples/linked-list.scm

28 lines
855 B
Scheme

(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))