Subject: Re: bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong

From: Tassilo Horn <[email-masked]>
Date: Tue, 24 Dec 2013 10:37:06 +0100

Tassilo Horn <[email-masked]> writes:

> ELISP> (evaluate '(fn x (add 1 x)) nil)
> (lambda
>   (val)
>   (evaluate body
>             (cons
>              (cons arg val)
>              env)))
>
> But shouldn't `arg' be substituted with 'x and `body' with '(add 1 x)?

It seems the `lambda' is the problem that prevents substitution of arg
and body.  By doing some kinda strange quoting I can get it right, but
IMHO that shouldn't be needed:

--8<---------------cut here---------------start------------->8---
(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))))
--8<---------------cut here---------------end--------------->8---

ELISP> (evaluate '(fn x (add 1 x)) nil)
(lambda
  (val)
  (evaluate
   '(add 1 x)
   (cons
    (cons 'x val)
    env)))

ELISP> (evaluate '(call (fn x (add 1 x)) 3) nil)
4 (#o4, #x4, ?\C-d)

Bye,
Tassilo