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

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

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

Post by joeish80829 » Wed May 07, 2014 3:39 am

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

Code: Select all

REPL> cv::b
500
I need a function with one parameter like this:

Code: Select all

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

Code: Select all

REPL> A

500

Can someone help me do this?

porky11
Posts: 25
Joined: Fri May 02, 2014 6:46 am

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

Post by porky11 » Wed May 07, 2014 9:51 am

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

Code: Select all

(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

Code: Select all

(foo 'b)

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

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

Post by joeish80829 » Thu May 08, 2014 1:19 am

Thank you very much that worked perfect

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

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

Post by Goheeca » Sat May 10, 2014 2:34 pm

Or more programatically or how to say. Not involving the read function.

Code: Select all

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

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

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

Post by joeish80829 » Sun May 11, 2014 1:12 am

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

Post Reply