how to prevent unwanted eval?

Q&A's, tips, howto's
Locked
lyl
Posts: 44
Joined: Sun Mar 25, 2018 5:00 am

how to prevent unwanted eval?

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

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: how to prevent unwanted eval?

Post 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
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

Locked