[plt-scheme] lambda-apply-values

From: Jos Koot (jos.koot at telefonica.net)
Date: Fri Nov 21 07:30:20 EST 2008

  ----- Original Message ----- 
  From: Stephen De Gabrielle 
  To: PLT List 
  Sent: Friday, November 21, 2008 1:07 PM
  Subject: [plt-scheme] lambda-apply-values


  Hi, 

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

  eg

  #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 (x y z))
      (λ (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.
May be 

(define-syntax lambda-apply-values-person2
  (syntax-rules ()
   ((_ ((values-list (e ...)) ...) . body)
    (λ (values-list ...)  
      (let-values ([(e ...)  (apply values values-list)] ...)
        . body)))))

(map (lambda-apply-values-person2 ((lsta (a b c)) (lstx (x y))) (list a b c x y))
 '((a0 b0 c0) (a1 b1 b2)) '((x0 y0) (x1 y1))) ;--> ((a0 b0 c0 x0 y0) (b0 b1 b2 x1 y1))


Jos

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

  Cheers,

  Stephen





------------------------------------------------------------------------------


  _________________________________________________
    For list-related administrative tasks:
    http://list.cs.brown.edu/mailman/listinfo/plt-scheme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20081121/935ed63e/attachment.html>

Posted on the users mailing list.