Macors that write functions/macros?

Started by skibud2, August 04, 2006, 06:32:18 AM

Previous topic - Next topic

skibud2

In clisp, you can do something like this:


(defun create-symbol (str)
  (intern (string-upcase str)) )


(defmacro create-functions (group-name)
  (let ((f1 (create-symbol
             (format nil "~a~a" group-name 1)))
        (f2 (create-symbol
             (format nil "~a~a" group-name 2))) )
    `(progn
       (defun ,f1 (arg) (+ arg 1))
       (defun ,f2 (arg) (+ arg 2)) ) ) )


if you call:

(create-functions foo)





it generates



(defun foo1 (arg) (+ arg 1)) and

(defun foo2 (arg) (+ arg 2))



Can you do something similar in newLisp??

Lutz

#1
like this:
(define-macro (create-functions group-name)
    (letex ( (f1 (sym (append (name group-name) "1")))
             (f2 (sym (append (name group-name) "2"))))
       (define (f1 arg) (+ arg 1))
       (define (f2 arg) (+ arg 2))))


now call:
> (create-functions foo)

> foo1
(lambda (arg) (+ arg 1))
> foo2
(lambda (arg) (+ arg 2))
> (foo1 10)
11
> (foo2 10)
12


'letex' in newLISP is a 'let' which does variable expansion first. The function 'sym' is used to create the symbol. 'name' returns the string representation of a symbol.



Lutz



Ps: instead of (append (name group-name) "1") you could also do (string group-name 1) this would be shorter but would not work correctly in  a name space different from MAIN, because 'string' would also return the name space prefix.