A default function looks and behaves similar to statically scoped functions found in other programming languages. Several functions can share one namespace. Using the built-in primitive def-new, a function or macro can be defined to create other functions enclosed in their own namespace:
Using the built-in primitive def-new, a function or macro can be defined to create other statically scoped functions:
(define (def-static s body) (def-new 'body (sym s s))) (def-static 'acc (lambda (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 function works by creating a context and default functor from the name of the function. def-static should only be used while in name space MAIN.
Using a more complex method, a def-static can be defined as a macro which can be used like the normal define function:
;; 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))))) (def-static (acc 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 macro def-static first creates a lambda expression of the function to be defined in the current name space and assignes it to the variable temp. In a second step the lambda function in temp is copied to it own name space. This happens by assigning it to the default functor acc:acc symbol built from the name of the function.