Convert list to variable arguments?

Started by Tim Johnson, March 11, 2008, 06:15:02 PM

Previous topic - Next topic

Tim Johnson

Let's suppose I have a function foo
(define (foo)
    (doargs (i) (println i)))

and I can process it by something like this:
(foo a b c x e f g)

Now, suppose I have a list that looks like this:
(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
Programmer since 1987. Unix environment.

Jeff

#1
(cons foo lst)
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

Lutz

#2
like this:


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

Tim Johnson

#3
Quote from: "Jeff"(cons foo lst)

> (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
Programmer since 1987. Unix environment.

Lutz

#4
we posted together ;-) the answer again:


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

Tim Johnson

#5
Quote from: "Lutz"we posted together ;-) the answer again:


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

Yes! Thank you sir!
Programmer since 1987. Unix environment.

Lutz

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


(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'.