difference result by eval in macro and in S-expr

Q&A's, tips, howto's

difference result by eval in macro and in S-expr

Postby lyl » Sat Apr 27, 2019 11:07 pm

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
lyl
 
Posts: 37
Joined: Sun Mar 25, 2018 5:00 am

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

Postby ralph.ronnquist » Sun Apr 28, 2019 1:52 am

mmm, did you check http://www.newlisp.org/downloads/newlisp_manual.html#define-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
ralph.ronnquist
 
Posts: 209
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

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

Postby lyl » Thu Oct 03, 2019 8:18 am

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?
lyl
 
Posts: 37
Joined: Sun Mar 25, 2018 5:00 am

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

Postby cameyo » Thu Oct 03, 2019 6:22 pm

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
cameyo
 
Posts: 87
Joined: Sun Mar 27, 2011 3:07 pm

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

Postby lyl » Thu Oct 03, 2019 9:59 pm

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"?
lyl
 
Posts: 37
Joined: Sun Mar 25, 2018 5:00 am

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

Postby cameyo » Fri Oct 04, 2019 9:22 am

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
cameyo
 
Posts: 87
Joined: Sun Mar 27, 2011 3:07 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest