This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

How do I create a variables name from scratch and run it

5 posts · 5295 views

Normally I can do (defparameter N 1) and run "N" at the repl and it would output 1.
Well I have variables from another package I need to setf to the variable "A"(for example)...The variables can be anything but they all start with the prefix "cv::" w/o quotes eg cv::b, cv::test,
cv::x ...Lets say the variable cv::b equals 500,

so
REPL> cv::b
500
I need a function with one parameter like this:
(defun foo (x)
"other stuff here"
)
so if I run (foo b) it will set the variable cv::b that is already created and equals 500 to a new variable "A" in side the function. so after I runn (foo b). I can evaluate "A" at the REPL and the output would be"
REPL> A

500

Can someone help me do this?

Re: How do I create a variables name from scratch and run i

I'm not sure, what you want to do.
If you define a function, b is evaluated, so you should use a macro or quote the b.

if you want to set the value of a to the value of cv::b you can write this function
(defun foo (x)
  "do something that makes variable a global, if it is not"
  (setq a (symbol-value (read-from-string (format nil "cv::~a" x)))))
You have to call
(foo 'b)

Re: How do I create a variables name from scratch and run i

Thank you very much that worked perfect

Re: How do I create a variables name from scratch and run i

Or more programatically or how to say. Not involving the read function.
(defun foo (x)
  (setf a (symbol-value (intern (symbol-name x) (find-package :cv)))))
Due to this exercise I found out that symbol-package is not setfable hence I used intern.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: How do I create a variables name from scratch and run i

Thanks...I ended up just creating a different .asd file and and that solved the issue...That's why I was trying to get this info to convert a variable from 1 package name to another