[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

readable code to SET recordfields



To: scheme@zurich.ai.mit.edu
Cc:
Subject: readable code to SET recordfields
From: Edward Hoenkamp <EDH at HNYKUN52.BITNET>
      (Arpa: EDH%HNYKUN52.BITNET@CUNYVM.CUNY.EDU)
Date: Fri May 13 13:20:13 1988
---


;;; What about this simple way of constructing and selecting records
;;; that will allow the fields to be globally 'set' even if the
;;; record is referred to by a variable.
;;; Edward Hoenkamp.

(define (make-bar struct)
      (lambda (select . new-value)
        (if (null? (car new-value))  ; if no new-value passed
            (car (select struct))    ; select and leave as is
            (set-car! (select struct) (caar new-value)))))

;;; choose appropriate selector names
(define (first:of name . new) (name (lambda(x) x) new))
(define (second:of name . new) (name cdr new))
(define (third:of name . new) (name cddr new))
;;; etcetera.

;;; Example
;   >>> (define foo (make-bar '(a b c)))
;   foo
;   >>> (first:of foo)
;   a
;   >>> (second:of foo)
;   b
;   >>> (define baz '(d e))
;   baz
;   >>> ((lambda (x)
;          (second:of x baz)) foo)
;   ((d e) c)
;   >>> (third:of foo)
;   c
;   >>> (second:of foo)
;   (d e)
;
;;; Add your own syntactic sugar for macro expansion
;;; (and/or use vectors instead of lists).