Convert list to variable arguments?

Notices and updates
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Convert list to variable arguments?

Post by Tim Johnson »

Let's suppose I have a function foo

Code: Select all

(define (foo)
    (doargs (i) (println i)))
and I can process it by something like this:

Code: Select all

(foo a b c x e f g)

Now, suppose I have a list that looks like this:

Code: Select all

(set 'lst '(a b c x e f g))
Is there a method to pass 'lst to 'foo so that it is consumend
one item at a time by 'doargs?
Thanks
tim

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

(cons foo lst)
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

like this:

Code: Select all

> (apply foo lst)
a
b
c
x
e
f
g
g
> 

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post by Tim Johnson »

Jeff wrote:(cons foo lst)

Code: Select all

> (cons foo lst)
((lambda () 
  (doargs (i) 
   (println i))) a b c x e f g)
;; That doesn't evaluate 'foo with 'lst...
;; Or did I miss something?
Thanks
tim

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

we posted together ;-) the answer again:

Code: Select all

> (apply foo lst)
a
b
c
x
e
f
g
g
> 

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post by Tim Johnson »

Lutz wrote:we posted together ;-) the answer again:

Code: Select all

> (apply foo lst)
a
b
c
x
e
f
g
g
> 
Yes! Thank you sir!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Jeff just forgot to wrap an 'eval' around his answer, but there is a difference between both approaches:

Code: Select all

(eval (cons foo lst))
This would give all nil's because a,b,c,d,e,f,g would be evaluated to their contents. It depends what you want, the contents of the symbols a to g or the symbols a to g themselves when using 'apply'.

Locked