Page 1 of 1

how to prevent unwanted eval?

Posted: Sun Aug 30, 2020 12:35 am
by lyl
I use a function "f" to store data by calling another function "g" like this:

Code: Select all

(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,

Code: Select all

(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?

Re: how to prevent unwanted eval?

Posted: Mon Aug 31, 2020 1:16 pm
by newBert
One possibility, probably among others:

Code: Select all

> (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