Mark Polesky <markpole...@yahoo.com> writes: >> (define (my-format bool string . list-of-nums) >> (apply format bool string (map set-precision list-of-nums))) >> >> (let ((a 2.0) >> (b 1/4) >> (c 1/3)) >> (my-format #t "~&~a ~a ~a" a b c)) > > > Kjetil, > > Awesome. Thanks. I knew there was something simple, > I just couldn't figure it out. That little dot helps > alot! And now for my next question... > > Is there a way for a pair within an alist to retrieve > values from other pairs within the same alist? I know > the following code doesn't work, because "my-alist" > is still undefined when I'm calling it, but is there > a way to make it do what I want? > > (define my-alist > `((a 1) > (b ,(+ 1 (cadr (assq 'a my-alist))))))
(define-macro (make-dalist . forms) (let ((alist-var (gensym))) `(letrec ((,alist-var (list)) (slot (lambda (key) (cdr (assq key ,alist-var))))) ,@(map (lambda (form) `(set! ,alist-var (acons ,(car form) ,(cdr form) ,alist-var))) forms) ,alist-var))) (pp (macroexpand-1 '(make-dalist ('a . 2) ('b . (+ 1 (slot 'a)))))) => (letrec ((#{\ g8}# (list)) (slot (lambda (key) (cdr (assq key #{\ g8}#))))) (set! #{\ g8}# (acons (quote a) 2 #{\ g8}#)) (set! #{\ g8}# (acons (quote b) (+ 1 (slot (quote a))) #{\ g8}#)) #{\ g8}#) (define foo (make-dalist ('a . 2) ('b . (+ 1 (slot 'a))))) => ((b . 3) (a . 2)) This only works when defining the alist, and assumes that you are only referencing earlier entries. Perhaps useful for a limited set of things. If you want something that updates dynamically then you really need a different data structure. It could be done ad hoc using manually defined accessors (e.g. 'b would not *really* exist, but would just be a function that calls `(+ 1 ...)'), or in the fancy general way by building a dataflow library (or, even more generally, a constraint solver). -- emacsen: every copy of Emacs comes with a bag of pot and 5 hits of acid emacsen: and a hotel coffee maker