yes there is a way, but let me first explain what is going on here:
Code: Select all
(define-macro (make-foo1)
(context 'FOO1)
(define (foo1) (setq var1 123)))
The context statement switches the context during runtime executing 'make-foo1', but at this time all other symbols in 'make-foo1' are already created as symbols in MAIN when reading the source from the program file. So at this time the context switch doesn't do anything anymore. newLISP reads a toplevel expression then evaluates it then reads the next toplevel expressin etc. While rading all symbols are created for the current context swithced to with a top level context statement.
'context' changes the context only for subsequent source code translation. When switching the context during runtime of 'make-foo1' it would only change the subsequent behaviour of an 'eval-string' or a 'sym' statement (if they don't have a context specified).
To create a function for a different context and that context at the same time you can use the following function described here:
http://newlisp.org/downloads/newlisp_ma ... al_scoping
Code: Select all
;; define static functions (use only in context MAIN)
;;
;; Example:
;;
;; (def-static (foo x) (+ x x))
;;
;; foo:foo ? (lambda (foo:x) (+ foo:x foo:x))
;;
;; (foo 10) ? 20
;;
(define-macro (def-static)
(let (temp (append (lambda) (list (1 (args 0)) (args 1))))
(def-new 'temp (sym (args 0 0) (args 0 0)))))
It is defined as a hygienic macro. Easier to understand perhaps this older version from the 8.7.1 manual, which does the same:
Code: Select all
(define (def-static s contents)
(def-new 'contents (sym s s)))
(def-static 'acc (fn (x) (if sum (inc 'sum x) (set 'sum x))))
(acc 5) => 5
(acc 5) => 10
(acc 2) => 12
acc:sum => 12
acc:x => nil
The current definition from the link just mentioned lets you use 'def-static' like 'define'.
So the link and the older previous example show how to define a function (actually the default function) for another context.
Doing a function define for the same current context is much more streight forward:
Code: Select all
(define-macro (make-func func body)
(set func body))
(make-func foo (fn (x) (+ x x)))
(foo 10) => 20
You just do an assignment of a lambda/fn expression to a symbol, which is the same as a 'define'.
If you are more interested in code to write functions in newLISP you should definitely also look into:
http://newlisp.org/downloads/newlisp_manual.html#expand
and
http://newlisp.org/downloads/newlisp_manual.html#letex
In newLISP variable expansion is something you do explicitly with 'expand' or 'letex'. You can use both to fill in function templates, which I think is what you want to do.
'define-macro' alone is just a 'define' without argument evaluation.
Lutz