how to prevent unwanted eval?

Started by lyl, August 29, 2020, 05:35:36 PM

Previous topic - Next topic

lyl

I use a function "f" to store data by calling another function "g" like this:
(define (g (x y)) x)
(define (f data)
  (setf (nth '(0 0 1) g) data)
  )
 
;;test:
(f 1)
(g) ;; -> 1  Right. This is what I want.
(g) ;; -> 1  Right. This is what I want.
(f '(+ 1 2))
(g) ;; -> 3  This is not what I want. I can't understand why the quoted list '(+ 1 2) is evaluated?. I just want to get the list itself '(+ 1 2)


By contrast,
(define (g x) x)
(define (f data) (g data))
(f '(+ 1 2)) ;; -> (+ 1 2)   The list is not evaled.


Is there a better way to prevent this kind of unwanted eval during the parameter transfer?

newBert

#1
One possibility, probably among others:


> (define (g (x nil)) x)
(lambda ((x nil)) x)
> (define-macro (f) (setf (nth '(0 0 1) g) (args 0)))
(lambda-macro () (setf (nth '(0 0 1) g) (args 0)))
> (f 1)
1
> (g)
1
> (f '(+ 1 2))
'(+ 1 2)
> (g)
(+ 1 2)


P.S.: I preferred (args 0) instead of 'data' for macro 'f' to avoid variable capture in passed parameters
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>