On Calling and Applying Newlisp Functions and Macros.

;---------------------------------------------------------------
; I continue with my researches of the Newlisp basics. It might be
; boring to some of the readers, but there will be plenty of
; time for other topics.
;
; The macros and the functions are, due to dynamic scoping, more
; similar in Newlisp than in other languages from Lisp family. The
; difference is only in the way arguments are passed to these two.

(set 'x 10 'y 20)

((lambda()(println (args)))
     1 2 "a" x y + - (+ x y)
     '1 '2 '"a" 'x 'y '+ '-  '(+ x y))
((lambda-macro()(println (args)))
     1 2 "a" x y + - (+ x y)
     '1 '2 '"a" 'x 'y '+ '-  '(+ x y))

; RESULTS:

;(1 2 "a" 10 20 + <40C365> - <40C380> 30 1 2 "a" x y + - (+ x y))
;(1 2 "a" x y + - (+ x y) '1 '2 '"a" 'x 'y '+ '- '(+ x y))

; (arg) in the CALLED function is list of evaluated arguments
; (arg) in the CALLED macro is list of unevaluated arguments

;---------------------------------------------------------------
; However, APPLYING of functions and macros on list works slightly
; different. Let as suppose that function and macro are APPLIED
; on list L.

(set 'L '(1 2 "a" (lambda()) x y + - (+ x y)
         '1 '2 '"a" 'x 'y '+ '-  '(+ x y)))
         
(apply (lambda()(push 4 (args))(println(args)))
       L)

(apply (lambda-macro()(println(args)))
       L)

; RESULTS:

;(1 2 "a" x y + - (+ x y) '1 '2 '"a" 'x 'y '+ '- '(+ x y))
;(1 2 "a" 'x 'y '+ '- '(+ x y) ''1 ''2 ''"a" ''x ''y ''+ ''- ''(+ x y))

;(args) in APPLIED function is copy of the L.
;       it is not original L; I tested that.

;(args) in APPLIED macro is OPTIMIZED list of the quoted elements of L
;       where OPTIMIZED means that "self-evaluating" elements are COPIED,
;       not quoted.

; That optimization in macro application is not harmful, because it
; holds that, inside APPLIED macro, (map eval (args)) is equal to L.

(apply (lambda-macro()(println (= L (map eval (args))))) L) ; true

No comments:

Post a Comment