Subject: 24.5: elisp manual about pcase
From: Zhaohui Li <[email-masked]>
To: [email-masked]
Date: Fri, 26 Feb 2016 10:08:09 +0800
List-Id: <debbugs-submit.debbugs.gnu.org>

The "10.2.1: Pattern matching case statement" in Emacs Lisp Referrence
Manual has a problem.
The second example in manual is:

>        (defun evaluate (exp env)
>        (pcase exp
>          (`(add ,x ,y)       (+ (evaluate x env) (evaluate y env)))
>          (`(call ,fun ,arg)  (funcall (evaluate fun env) (evaluate arg
> env)))
>          (`(fn ,arg ,body)   (lambda (val)
>                                (evaluate body (cons (cons arg val) env))))
>          ((pred numberp)     exp)
>          ((pred symbolp)     (cdr (assq exp env)))
>          (_                  (error "Unknown expression %S" exp))))
>

The problem is about the third case: `(fn , arg ,body).
I test this function with:

> (evaluate '(call
>             (fn x (add 1 x))
>             2)
>           nil)
>
emcas eval it with throw errors.

I think the correct of this function should be:

> (defun evaluate (exp env)
>   (pcase exp
>     (`(add ,x ,y)
>      (+ (evaluate x env)
>         (evaluate y env)))
>     (`(call ,fun ,arg)
>      (funcall (evaluate fun env)
>               (evaluate arg env)))
>     (`(fn ,arg ,body)
>      `(lambda (val)
>         (evaluate ',body (cons (cons ',arg val) env))))
>     ((pred numberp)
>      exp)
>     ((pred symbolp)
>      (cdr (assq exp env)))
>     (_
>      (error "Unknown expression %S" exp))))
>

Thanks~