[erlang-questions] LFE - Lisp Flavoured Erlang released

Robert Virding rvirding@REDACTED
Thu Mar 6 22:39:43 CET 2008


On 06/03/2008, Yariv Sadan <yarivsadan@REDACTED> wrote:
>
> Hi, I think I found a bug in lfe_eval.
>
> I created the following macro:
>
> (define-syntax bar
>   (macro
>    ((vals)
>     (: lists map (lambda (x) (: io format '"~p~n" x)) vals) ())))
>
> and the following function:
>
> (define (foo)
>   (bar (1 2 3)))
>
> When I call '(foo)' I get the following error:
>
> ** exception error: lfe_eval:'-eval_lambda/2-fun-0-'/2 called with one
> argument
>      in function  lists:map/2
>      in call from lfe_eval:eval_body/2
>      in call from lfe_macro:macro/3
>      in call from lfe_macro:expand_tail/3
>      in call from lfe_macro:expand/2
>      in call from lfe_macro:expand_top_forms/2
>      in call from lfe_macro:expand_top_forms/2
>      in call from lfe_macro:expand_file/2


That's a  bug, but it has now been fixed. I have released a patch on
trapexit.org with a fix and a few other goodies.

Also, can you please explain the difference between 'macro' and
> 'syntax-rules'? 'macro' seems to do what I expect and I'm not sure
> when syntax-rules would be a better option.


'syntax-rules' are simpler and are taken from Scheme. Each rule consists of
(pattern expansion) where pattern is matched against the arguments of the
macro call and the values of the variables from pattern are substituted into
the expansion which is then returned. It is like a simple form of backquote
macro where the only possible unquoting is symbol names. You can't "do"
anything in the pattern except return it. So for example a simple recursive
definition of let* (sequential let) would be:

(define-syntax let*
  (syntax-rules
    ([(vb . vbs) . b] [let (vb) (let* vbs . b)])
    ([() . b] [begin . b])))

Here I also use [...] as alternative to (...) to mark out the patterns and
the expansion.

Syntax-rules are useful when the macro can be written as a simple expansion,
although there is not much difference in using macro with a simple
backquote. See test_macro.lfe for an example of the same macro written in
both ways. Macro needs the evaluator to evaluate the body while syntax-rules
does not.

I am thinking of changing the names to defsyntax and defmacro :

(defsyntax let*
  (pat1 exp1)
  ... )

(defmacro let*
  (pat1 . body1)
  ... )

What does the "user community" feel about that? Having both under one
define-syntax maybe a clearer way to express the intention though.

Thanks,
>
> Yariv
>

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080306/b64c1825/attachment.htm>


More information about the erlang-questions mailing list