[plt-scheme] lambda-apply-values

From: Stephen De Gabrielle (spdegabrielle at gmail.com)
Date: Fri Nov 21 07:07:52 EST 2008


something I seem to often do is use list as structures, (and have lists of


#lang scheme
(define  people '(("Guy" 40 man) ("Maddona" 50 woman)))

I am often doing something like;

(map (λ (person)
(let ((name (car person))
        (age (car person))
        (gender (car person)))
   (blah-fn name age gender)
 ) people)

I could, but don't often, do (map (λ (name age gender) (blah-fn name age
gender) ) (map car people) (map cadr people) (map caddr people))

I realise this is a bit rubbish and have thought of a better way;

(map (λ (person)
       (let-values ([(name age gender)  (apply values person)])
         (blah-fn name age gender)
         )) people)

I sort of suspect that a macro might be the ticket - like;

(define-syntax-rule (lambda-apply-values-person1 values-list blah-fn x y z)
  (λ (values-list)
    (let-values ([(x y z)  (apply values values-list)])
      (blah-fn x y z)

(map (lambda-apply-values-person1 person (lambda args (display args)) x y
z)  people)

which works but doesn't handle multiple identifiers;

(define-syntax lambda-apply-values-person2
  (syntax-rules ()
    (lambda-apply-values-person2 values-list ...)
    (λ (values-list)
      (let-values ([(...)  (apply values values-list)])
        (list ...)
of course this doesn't work. I suspect I can't do this with syntax-rules and
need to look at syntax-case and the syntax form.

Because I do this all the time I'm sure that others have a better way. What
do you do?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20081121/52c4c18b/attachment.html>

Posted on the users mailing list.