fexpr issues

Started by Astrobe, February 28, 2014, 01:42:33 AM

Previous topic - Next topic

Astrobe

#15
I know this is different from what I initially asked for, but...



if you remove lines from 4912 to 4921 (the ones that insert the expansion code), one gets interesting options for forging the replacement expression.

Like for instance this improved curry:



(macro (curry*)
  (case (length $args)
    (2 (letex (F (args 0) A (args 1)) (fn(x) (F A x))))
    (3 (letex (F (args 0) A (args 1) B (args 2)) (fn(x) (F A B x))))))

(set 'K (curry* + 1))
> (lambda (x) (+ 1 x))
(set 'L (curry* + 1 2))
>(lambda (x) (+ 1 2 x))


(copied by hand from another screen; parens might be missing)



What I'm thinking is the functionality of "macro" already exists in a module, and the term "macro" can be confusing together with define-macro, so... What if we call our new feature "inline" (for instance), and we don't restrict it to the functionality of its ancestor?

Lutz

#16
The module implementation of macro in macro.lsp added more than 100% of load-time for each macro defined. So when you had 10 macros defined, the whole load time of source files, would increase by more than 10 times.



The new native implementation has no measurable impact on load times, even when defining many macros.



You are still able to do, what you are showing in your post. The default expansion function in C in lines 4912 to 4921 can easily be overwritten once you have marked the symbol as a macro symbol:



; let newLISP know that curry* contains a reader expansion function
> (macro (curry*))
(lambda-macro () (expand 'nil))

; now assign your own for that same symbol
(define curry* (lambda-macro ()
  (case (length $args)
    (2 (letex (F (args 0) A (args 1)) (fn(x) (F A x))))
    (3 (letex (F (args 0) A (args 1) B (args 2)) (fn(x) (F A B x)))))))

> (set 'K (curry* + 1))
(lambda (x) (+ 1 x))
> (set 'L (curry* + 1 2))
(lambda (x) (+ 1 2 x))
>


What the native implementation of macro gives us, is more reading speed. But we still have the possibility to play our own tricks. The real work - reader-time expansion - happens in lines 3505 to 3515.



You also could code your example without the technique shown above in one step like this:



(macro (curry* F A B)
    (if B (lambda (x) (F A B x)) (lambda (x) (F A x))))

> (curry* + 1)
(lambda (x) (+ 1 x))
> (curry* + 1 2)
(lambda (x) (+ 1 2 x))

Lutz

#17
More explanations how the new macro works with map and apply and differences to define-macro:



http://www.newlisp.org/downloads/development/inprogress/newlisp_manual.html#macro">http://www.newlisp.org/downloads/develo ... html#macro">http://www.newlisp.org/downloads/development/inprogress/newlisp_manual.html#macro



Any corrections are appreciated.

bairui

#18
Hi, Lutz,



In the last example box for (macro ...):


(define-macro (fexpr-double X) i

Trailing i a vim oopsie? :-)