Page 1 of 1

difference result by eval in macro and in S-expr

Posted: Sat Apr 27, 2019 11:07 pm
by lyl
I think the following two examples say the same thing, but they give different results, why?

Example No. 1:

Code: Select all

(define-macro (m lst)
  (eval lst))
(m '(+ 4 5)) ;; -> (+ 4 5)
Example No. 2:

Code: Select all

(eval '(+ 4 5)) ;; -> 9

Re: difference result by eval in macro and in S-expr

Posted: Sun Apr 28, 2019 1:52 am
by ralph.ronnquist
mmm, did you check http://www.newlisp.org/downloads/newlis ... fine-macro ?

The key point is that for m, its parameter lst gets bound to the un-evaluated argument '(+ 4 5), whereas in example 2 the eval function's parameter gets the evaluated argument (+ 4 5), i.e, without the single-quote. In other words,

Code: Select all

(m '(+ 4 5))
is actually the same as

Code: Select all

(eval ''(+ 4 5))
If you'd want an "m" function to be the same as an "eval" function, you'd use "define" rather than "define-macro". (Not totally the same, since "m" binds "lst" before calling "eval", but that's a separate concern)

hth

Re: difference result by eval in macro and in S-expr

Posted: Thu Oct 03, 2019 8:18 am
by lyl
Then, in the following example

Code: Select all

(define(f lst)
  (+ lst 1))

(setq a '(+ 2 3)) 
(f a) 
As arguments of function in lisp are evaled first, I think (f a) is the same as (+ (+ 2 3) 1) which should be 6, but what I get is: "ERR: value expected in function + : (+ 2 3)". Why?

Re: difference result by eval in macro and in S-expr

Posted: Thu Oct 03, 2019 6:22 pm
by cameyo
Maybe you can use letex:

Code: Select all

(define(f lst)
  (letex (x lst) (+ x 1)))

(setq a '(+ 2 3))
;-> (+ 2 3)

(f a)
;-> 6
Try this:

Code: Select all

(define types '("nil" "true" "int" "float" "string" "symbol" "context"
    "primitive" "import" "ffi" "quote" "expression" "lambda" "fexpr" "array"
    "dyn_symbol"))

(define (typeof v)
    (types (& 0xf ((dump v) 1))))

(define(f lst)
  (letex (x lst) 
    (println (list? lst))
    (println (symbol? lst))
    (println (list? x))
    (println (symbol? x))
    (println (dump lst))
    (println (dump x))
    (println (typeof lst))
    (println (typeof x))
  (+ x 1)))

(setq a '(+ 2 3))
;-> (+ 2 3)

(f a)
;-> true
;-> nil
;-> nil
;-> nil
;-> (7026864 59 7012912 7030672 7026896)
;-> (7030096 386 7012912 7012912 5)
;-> expression
;-> int
;-> 6

Re: difference result by eval in macro and in S-expr

Posted: Thu Oct 03, 2019 9:59 pm
by lyl
Thank you, cameyo. I think the argument "lst" of "f" has been evaled during the call of (f a), so why must it be evaled again by "letex" in function body ?
And could you please give more information about the function "typeof"?

Re: difference result by eval in macro and in S-expr

Posted: Fri Oct 04, 2019 9:22 am
by cameyo
Hi lyl,
i'm not an expert, but i think "evaluated" is different from "expanded". Maybe a guru will highligth this question.
The typeof function show the type of the argument; the core function is dump (see the manual for information).
ciao