Macors that write functions/macros?

Notices and updates
Locked
skibud2
Posts: 1
Joined: Fri Aug 04, 2006 1:21 pm

Macors that write functions/macros?

Post by skibud2 »

In clisp, you can do something like this:

Code: Select all

 (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

like this:

Code: Select all

(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:

Code: Select all

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

Locked