aboutsummaryrefslogtreecommitdiffstats
path: root/examples/linked-list.scm
blob: 821d0b6ca8b0be49894f35bf3185ee68edc0b937 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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))