Page 1 of 1

Convert list to variable arguments?

Posted: Wed Mar 12, 2008 1:15 am
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

Posted: Wed Mar 12, 2008 1:19 am
by Jeff
(cons foo lst)

Posted: Wed Mar 12, 2008 1:31 am
by Lutz
like this:

Code: Select all

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

Posted: Wed Mar 12, 2008 1:33 am
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

Posted: Wed Mar 12, 2008 1:35 am
by Lutz
we posted together ;-) the answer again:

Code: Select all

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

Posted: Wed Mar 12, 2008 1:37 am
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!

Posted: Wed Mar 12, 2008 1:44 am
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'.