newLISP Fan Club

Forum => newLISP newS => Topic started by: Tim Johnson on March 11, 2008, 06:15:02 PM

Title: Convert list to variable arguments?
Post by: Tim Johnson on March 11, 2008, 06:15:02 PM
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
Title:
Post by: Jeff on March 11, 2008, 06:19:31 PM
(cons foo lst)
Title:
Post by: Lutz on March 11, 2008, 06:31:48 PM
like this:


> (apply foo lst)
a
b
c
x
e
f
g
g
>
Title:
Post by: Tim Johnson on March 11, 2008, 06:33:12 PM
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
Title:
Post by: Lutz on March 11, 2008, 06:35:42 PM
we posted together ;-) the answer again:


> (apply foo lst)
a
b
c
x
e
f
g
g
>
Title:
Post by: Tim Johnson on March 11, 2008, 06:37:02 PM
Quote from: "Lutz"we posted together ;-) the answer again:


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

Yes! Thank you sir!
Title:
Post by: Lutz on March 11, 2008, 06:44:08 PM
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'.