I am writing a macro in which the argument list ends with one or more lists. In my macro I am trying to get the index at which the lists start so I can slice them out. So I write something like this
(define-macro (func)
(setq cnt (first (index list? (args))))
)
but when I do this NewLISP squawks. What am I doing wrong?
Looks like it works a bit...
newLISP v.8.8.9 on OSX UTF-8, execute 'newlisp -h' for more info.
> (define-macro (func) (set 'cnt (first (index list? (args)))))
(lambda-macro () (set 'cnt (first (index list? (args)))))
> (func "1" (list (sequence 1 2 3)))
1
>
this works for me:
> (define-macro (func) (nth (first (index list? (args))) (args)))
(lambda-macro () (nth (first (index list? (args))) (args)))
> (func 1 2 (a b c))
(a b c)
> (func 1 2 (a b c) 4 5)
(a b c)
>
always returns the first list argument found. But you can make it shorter using 'filter':
> (define-macro (func) (first (filter list? (args))))
(lambda-macro () (first (filter list? (args))))
> (func 1 2 (a b c) 4 5)
(a b c)
>
or even shorter using implicit indexing:
> (define-macro (func) ((filter list? (args)) 0))
(lambda-macro () ((filter list? (args)) 0))
> (func 1 2 (a b c) 4 5)
(a b c)
>
Lutz
Deleted message - Lutz posted before I did - Thanks!
Still have a question about the lambda-macro statement and what that is doing. When I look in my function listings I see fn, lambda and lambda? but I don't find any reference to lambda-macro. Sorry to be so dense but what is lambda-macro and what is it doing to the previous statement?
'lambda-macro' has the same relation to 'define-macro' as 'lambda' has to 'define', but 'lambda-macro' is not present in the index/manual. It will be added. 'fn' is just a shorter form for writing 'lambda'.
Lutz