Using defparameter inside a function to make and bind a list of variables
This code is supposed to define symbols A B C, D, and E and assign values. However, the code does not work.
=========== Start of code =============
;;;; Using defparameter inside a function to make and
;;;; bind a list of variables
;;;;
;;;; define-variables is the function to call from the Listener.
;;;; define-variables-1 is the function under test
;;; recursively called function
(defun define-variables-1 (name-lst value-lst)
(let ((cname (first name-lst))
(cvalue (first value-lst)))
(cond (cname
(defparameter cname cvalue)
(return-from define-variables-1
(define-variables-1 (rest name-lst) (rest value-lst))))
(t
(return-from define-variables-1 nil)))))
;;; function that sets up the situation and calls
;;; the function under test
(defun define-variables ()
(define-variables-1 (list 'a 'b 'c 'd 'e)
(list 1 2 3 'x 'y)))
=========== End of code =============
Running the code gives these warnings:
Warning: (DEFVAR CNAME) defined more than once in D:\Users\Ron\Documents\LispWorks\AEM\try.lisp.
Warning: (DEFVAR CNAME) defined more than once in D:\Users\Ron\Documents\LispWorks\AEM\try.lisp.
Warning: (DEFVAR CNAME) defined more than once in D:\Users\Ron\Documents\LispWorks\AEM\try.lisp.
Warning: (DEFVAR CNAME) defined more than once in D:\Users\Ron\Documents\LispWorks\AEM\try.lisp.
The code could work with SET instead of DEFPARAMETER. But really, DEFPARAMETER is just an example. I could be trying to use DEFCONSTANT or DEFVAR and the code would not work.
I could make the code work using EVAL, READ, and FORMAT to create the desired string to execute on each iteration.
Using SETF as an example of the kind of solution I seek:
You can do this in LispWorks, even though (if I understand) “defining variables using SETF” is undefined in the Common Lisp Standard:
=============== Begin session ===============
CL-USER 1 > (setf var 'x)
X
CL-USER 2 > (setf (symbol-value var) 'y)
Y
CL-USER 3 > x
Y
CL-USER 4 > var
X
=============== End session ===============
While symbol-value works for SETF (in LispWorks), symbol-value does not work for DEFPARAMETER. The error is that (SYMBOL-VALUE CNAME) is not a symbol. Of course it is not a symbol, it is a list. So I see that this list does not get evaluated to a symbol for DEFPARAMETER.
So, is there something I can do more along the lines of what makes SETF work? Can I do something other than use EVAL, READ, and FORMAT to make DEFPARAMETER work inside the function for the lists?
Ron Lewis