git @ Cat's Eye Technologies define-opaque / master eg / proper-list.scm

Tree @master (Download .tar.gz)

proper-list.scm @masterraw · history · blame

; The content of this file is in the public domain.
; example usage with Chicken Scheme: csi -q -b proper-list.scm

(load "define-opaque-0.2.scm")

(define-opaque proper-list make-proper-list (selector value) ('nil 0)
    (proper-list? (lambda ()
    (nil (lambda ()
      (make-proper-list 'nil 0)))
    (cons (lambda (head-value tail-value)
      ; NB this is not hugely great, as it's a kind of duck-typing.
      (if (tail-value 'proper-list?)
        (make-proper-list 'cons (cons head-value tail-value))
        (error "Tail is not a proper list: " tail-value))))
    (repr (lambda ()
        ((equal? selector 'nil)
        ((equal? selector 'cons)
          (let* ((head (car value)) (tail (cdr value)))
            (cons head (tail 'repr)))))))

(define demo (lambda ()
  (let* (
    (list0 (proper-list 'cons 123 (proper-list 'cons 456 (proper-list 'nil))))
    (display (list0 'repr)) (newline)
    ;(display (proper-list 'cons 123 456))  ; this will error!