Advertisement
Guest User

Untitled

a guest
Jul 16th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.41 KB | None | 0 0
  1. #!r6rs
  2. [library [typed-lists [0 0 1]]
  3.     [export
  4.         typed-list-type?
  5.         typed-list?
  6.         define-list-type
  7.         typed-cons
  8.         typed-car
  9.         typed-cdr
  10.         ]
  11.     [import
  12.         [rnrs [6]]
  13.         ]
  14.  
  15. [define (typed-list? x)
  16.     [or (null? x) (typed-list?* x)]]
  17.  
  18. [define (typed-list-type? xs type)
  19.     [assert (typed-list? xs)]
  20.     [or (null? xs) (eqv? type (get-type xs))]]
  21.  
  22. [define-record-type [typed-list typed-cons typed-list?*]
  23.     [fields
  24.         [immutable car typed-car]
  25.         [immutable cdr typed-cdr]
  26.         [immutable type get-type]
  27.         ]
  28.  
  29.     [protocol
  30.         [lambda [make]
  31.             [lambda [car cdr type?]
  32.                 [assert (procedure? type?)]
  33.                 [assert (type? car)]
  34.                 [assert (typed-list? cdr)]
  35.                 [assert (typed-list-type? cdr type?)]
  36.                 (make car cdr type?)]]]]
  37.  
  38. [define-syntax define-list-type [lambda [stx]
  39.     [syntax-case stx []
  40.         [[define-list-type type?]
  41.             (identifier? #'type?)
  42.             [let* [
  43.                     [name     (symbol->string (syntax->datum #'type?))]
  44.                     [cons*    (datum->syntax #'define-list-type (string->symbol (string-append name "-cons")))]
  45.                     [list*    (datum->syntax #'define-list-type (string->symbol (string-append name "-list")))]
  46.                     [car*     (datum->syntax #'define-list-type (string->symbol (string-append name "-car")))]
  47.                     [cdr*     (datum->syntax #'define-list-type (string->symbol (string-append name "-cdr")))]
  48.                     [list?*   (datum->syntax #'define-list-type (string->symbol (string-append name "-list?")))]
  49.                     ]
  50.                 #`[begin
  51.                     [define (#,cons* x y) (typed-cons x y type?)]
  52.                     [define (#,list* . xs)
  53.                         [if (null? xs) xs
  54.                             (#,cons* (car xs) (apply #,list* (cdr xs)))]]
  55.                     [define (#,car* x)
  56.                         [assert (typed-list-type? x type?)]
  57.                         (typed-car x)]
  58.                     [define (#,cdr* x)
  59.                         [assert (typed-list-type x type?)]
  60.                         (typed-cdr x)]
  61.                     [define (#,list?* x)
  62.                         [and (typed-list? x) (typed-list-type? x type?)]]
  63.                     ]]]
  64.             ]]]
  65.  
  66. ]
  67.  
  68. ;; the empty typed-list is simply ()
  69.  
  70. ;; example usage:
  71.  
  72. [define-typed-list integer?]
  73. ;; this makes available integer?-cons, integer?-car, integer?-cdr, integer?-list, integer?-list?
  74.  
  75. [define an-int-list (integer?-list 1 2 3 4 5)]
  76. [define a-not-quote-intlist (integer?-list 1 2 3 4 5.1)] ;; ==> ERROR
  77.  
  78. (integer?-list? an-int-list) ;; ==> #t
  79.  
  80. [define-typed-list even?]
  81.  
  82. [define evens (even?-list 2 4 6)]
  83.  
  84. (integer?-list? evens?) ;; ==> #f, the check happens in constant time, it does not verify the contents of the list
  85.  
  86. (integer?-car an-int-list) ==> ERROR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement