How do I name a variable after a string in Lisp? - lisp

I'd like to rename a variable in Lisp, and use a pre-existing string for the renaming. I.e. if my string is "abc" I'd like Lisp to take the string and make it the name of my variable. Any ideas?
Thank you!

Based on your comment:
Here's what I'm trying to do: 1. I'm writing a parser for a new language. 2. Part of the language involves creating variables and naming them. 3. So, the user enters: name:contents -- where name is the name of the variable and contents is what the variable contains. 4. I'd like to know how to take the name input that the user provides, and tell Lisp to create a variable and call it name
You almost certainly do not want to use Lisp variables for whatever your embedded language is. Instead the usual thing to do in this case is to define some kind of 'environment' object which carries around a mapping between variable names and values in the language you are implementing. Typically you want the following operations on it.
Is a variable bound in an environment?
Retrieve the value of a bound variable from an environment.
Modify the value of a bound variable in an environment.
Extend an environment with one or more new bindings, returning a new environment.
And perhaps add a binding to an environment, without constructing a new environment.
If you can get away without the last one then that's generally good, as it will make the semantics of your language easier to understand and errors in programs easier to catch, as you can't assign to unbound variables.
A classic (but slow in some cases) example of an implementation of an environment structure is an association list. This easily supports the first three, but does not really support the last easily.
Here is an example which assumes variables are named by strings (so you don't need to intern symbols for them):
(defconstant empty-environment '())
(defun variable-bound-in-environment-p (env var)
(if (assoc var env :test #'string=) t nil))
(defun variable-value-in-environment (env var)
(let ((binding (assoc var env :test #'string=)))
(unless binding
(error "unbound"))
(cdr binding)))
(defun (setf variable-value-in-environment) (new env var)
(let ((binding (assoc var env :test #'string=)))
(unless binding
(error "unbound"))
(setf (cdr binding) new)))
(defun extend-environment (env &rest vars/vals)
(labels ((ee-loop (e vvp)
(if (null vvp)
e
(destructuring-bind (var val . more) vvp
(ee-loop (acons var val e) more)))))
(ee-loop env vars/vals)))
Now:
> (variable-bound-in-environment-p empty-environment "foo")
nil
> (variable-value-in-environment
(extend-environment empty-environment "foo" 1)
"foo")
1
> (variable-value-in-environment
(extend-environment
(extend-environment empty-environment "foo" 1)
"foo" 2)
"foo")
2
> (variable-value-in-environment
(let ((e (extend-environment empty-environment "foo" 1)))
(setf (variable-value-in-environment e "foo") 2)
e)
"foo")
2
You may want to use Lisp symbols for variable names, which will make variable lookup etc in environments a little faster and nicer. For these purposes symbols are simply strings with identity. To do this you need to take the strings you get as variable names and 'intern' them to create symbols. A good approach to this is to keep these symbols in their own namespace, which Common Lisp calls a 'package', rather than smashing the namespace that the underlying Lisp program uses:
(defvar *language-package* (make-package "LANGUAGE-PACKAGE" :use '()))
(defun intern-language-variable-name (s &optional (package *language-package*))
(intern s package))
And now you can be sure that
> (eq (intern-language-variable-name "foo")
(intern-language-variable-name "foo"))
t
And then you can, after getting the stringy versions of variable names from your parser, intern them using intern-language-variable-name and then use these objects in your environment, which can thus be a little faster and simpler:
(defconstant empty-environment '())
(defun variable-bound-in-environment-p (env var)
(if (assoc var env) t nil))
(defun variable-value-in-environment (env var)
(let ((binding (assoc var env)))
(unless binding
(error "unbound"))
(cdr binding)))
(defun (setf variable-value-in-environment) (new env var)
(let ((binding (assoc var env)))
(unless binding
(error "unbound"))
(setf (cdr binding) new)))
(defun extend-environment (env &rest vars/vals)
(labels ((ee-loop (e vvp)
(if (null vvp)
e
(destructuring-bind (var val . more) vvp
(ee-loop (acons var val e) more)))))
(ee-loop env vars/vals)))

Related

Is there an existing lisp macro for building up a list?

In Python, I am able to use yield to build up a list without having to define a temporary variable:
def get_chars_skipping_bar(word):
while word:
# Imperative logic which can't be
# replaced with a for loop.
if word[:3] == 'bar':
word = word[3:]
else:
yield foo[0]
foo = foo[1:]
In elisp, I can't see any way of doing this, either built-in or using any pre-existing libraries. I'm forced to manually build a up a list and call nreverse on it. Since this is a common pattern, I've written my own macro:
(require 'dash)
(require 'cl)
(defun replace-calls (form x func)
"Replace all calls to X (a symbol) in FORM,
calling FUNC to generate the replacement."
(--map
(cond
((consp it)
(if (eq (car it) x)
(funcall func it)
(replace-calls it x func)))
(:else it))
form))
(defmacro with-results (&rest body)
"Execute BODY, which may contain forms (yield foo).
Return a list built up from all the values passed to yield."
(let ((results (gensym "results")))
`(let ((,results (list)))
,#(replace-calls body 'yield
(lambda (form) `(push ,(second form) ,results)))
(nreverse ,results))))
Example usage:
(setq foo "barbazbarbarbiz")
(with-results
(while (not (s-equals? "" foo))
;; Imperative logic which can't be replaced with cl-loop's across.
(if (s-starts-with? "bar" foo)
(setq foo (substring foo 3))
(progn
(yield (substring foo 0 1))
(setq foo (substring foo 1))))))
There must be a better way of doing this, or an existing solution, somewhere in elisp, cl.el, or a library.
The Python function is actually a generator. In ANSI Common Lisp, we would usually reach for a lexical closure to simulate a generator, or else us a library to define generators directly, like Pygen. Maybe these approaches can be ported to Emacs Lisp.
AFAIK, people just use push+nreverse like you do. If you want to define your macro in a more robust way (e.g. so it doesn't misfire on something like (memq sym '(yield stop next))) you could do it as:
(defmacro with-results (&rest body)
"Execute BODY, which may contain forms (yield EXP).
Return a list built up from all the values passed to `yield'."
(let ((results (gensym "results")))
`(let ((,results '()))
(cl-macrolet ((yield (exp) `(push ,exp ,results)))
,#body)
(nreverse ,results))))
Maybe something like this:
(setq foo "barbaz")
(cl-loop for i from 0 to (1- (length foo))
collect (string (aref foo i)))
In any case, there's nothing wrong with push and nreverse.
Lisp is different from Python. yield is not used. I also see the use of coroutine-like constructs for this as a mistake. It's the equivalent of the come-from construct. Suddenly routines have multiple context dependent entry points.
In Lisp use functions/closures instead.
In Common Lisp, the LOOP macro allows efficient mappings over vectors. The following code can be abstracted to some mapping function, if preferred:
CL-USER 17 > (defun chars-without-substring (string substring)
(loop with i = 0
while (< i (length string))
when (and (>= (- (length string) i) (length substring))
(string= substring string
:start2 i
:end2 (+ i (length substring))))
do (incf i (length substring))
else
collect (prog1 (char string i) (incf i))))
CHARS-WITHOUT-SUBSTRING
CL-USER 18 > (chars-without-substring "barbazbarbarbiz" "bar")
(#\b #\a #\z #\b #\i #\z)

define variable with defparameter with name determined at runtime

I would like to initiate dynamically a hash table with defmethod or defun using one of the arguments to create the name. For instance:
(defun foo (arg)
(let ((NAME (read-from-string (format nil "\*~S\*" arg))))
(defparameter NAME (make-hash-table))))
Of course, foo create hash table with the symbol NAME, instead of the value of NAME in let. What can I do to get the value of NAME to create this hash table?
General Remarks
It is almost always wrong to create global variables in functions.
It is also almost always wrong to create new symbols using read-from-string instead of intern.
Use a Macro
What you probably want is
(defmacro def-ht (name)
(let ((var (intern (concatenate 'string "*" (symbol-name name) "*")
(symbol-package name))))
`(defparameter ,var (make-hash-table))))
(def-ht foo)
Use a Function
You might be able to do it in a function too - by inspecting the macroexpansion of a defparameter form and placing the needed stuff in the function:
(defun make-ht-var (name)
(let ((var (intern (concatenate 'string "*" (symbol-name name) "*")
(symbol-package name))))
(setf (symbol-value var) (make-hash-table))
(proclaim (list var 'special))))
(make-ht-var 'foo)
Note that the argument to the function is quoted, but the argument to the macro is not.
You need to use a macro instead of a function. DEFPARAMETER will bind value of MAKE-HASH-TABLE to the symbol NAME because it evaluates at macro-expansion time which occurs earlier than run-time, which is when the function FOO binds the lexical value of NAME.
Look up the CL evaluation model for a deeper understanding.
(defmacro foo (arg)
(let ((name (read-from-string (format nil "*~S*" arg))))
`(defparameter ,name (make-hash-table))))
(foo "my-hash")
=> <hash-table 0x121>
*my-hash*
=> <hash-table 0x121>

Function name and dynamic binding in Common Lisp

I'm reading Peter Norvig's Paradigms of AI. In chapter 6.2, the author uses code like below (not the original code, I picked out the troubling part):
Code Snippet:
(progv '(op arg) '(1+ 1)
(eval '(op arg)))
As the author's original intent, this code should return 2, but in sbcl 1.1.1, the interpreter is apparently not looking up op in the environment, throwing out op: undefined function.
Is this implementation specific? Since the code must have been tested on some other lisp.
p.s Original code
You probably mean
(progv '(op arg) '(1+ 1)
(eval '(funcall op arg)))
Edit(2013-08-21):
PAIP was written in pre-ANSI-Common-Lisp era, so it's possible the code
there contains a few noncompliances wrt the standard. We can make
the examples work with the following revision:
(defun match-if (pattern input bindings)
"Test an arbitrary expression involving variables.
The pattern looks like ((?if code) . rest)."
(and (eval (reduce (lambda (code binding)
(destructuring-bind (var . val) binding
(subst val var code)))
bindings :initial-value (second (first pattern))))
(pat-match (rest pattern) input bindings)))
;; CL-USER> (pat-match '(?x ?op ?y is ?z (?if (eql (?op ?x ?y) ?z))) '(3 + 4 is 7))
;; ((?Z . 7) (?Y . 4) (?OP . +) (?X . 3) (T . T))
;; CL-USER> (pat-match '(?x ?op ?y (?if (?op ?x ?y))) '(3 > 4))
;; NIL
Elements in first positions are not looked up as values, but as functions and there is no concept of dynamic binding in the function namespace.
I'd say after a quick look that the original code was designed to evaluate in a context like
(progv '(x y) '(12 34)
(eval '(> (+ x y) 99)))
i.e. evaluating a formula providing substitution for variables, not for function names.
The other answers so far are right, in that the actual form being evaluated is not the variables being bound by progv (simply (op arg)), but none have mentioned what is being evaluated. In fact, the comments in the code you linked to provide a (very) short explanation (this is the only code in that file that uses progv):
(defun match-if (pattern input bindings)
"Test an arbitrary expression involving variables.
The pattern looks like ((?if code) . rest)."
;; *** fix, rjf 10/1/92 (used to eval binding values)
(and (progv (mapcar #'car bindings)
(mapcar #'cdr bindings)
(eval (second (first pattern))))
(pat-match (rest pattern) input bindings)))
The idea is that a call to match-if gets called like
(match-if '((?if code) . rest) input ((v1 val1) (v2 val2) ...))
and eval is called with (second (first pattern)), which the value of code. However, eval is called within the progv that binds v1, v2, &c., to the corresponding val1, val2, &c., so that if any of those variables appear free in code, then they are bound when code is evaluated.
Problem
The problem that I see here is that, by the code we can't tell if the value is to be saved as the variable's symbol-value or symbol-function. Thus when you put a + as a value to some corresponding variable, say v, then it'll always be saved as the symbol-value of var, not it's symbol-function.
Therefore when you'll try to use it as, say (v 1 2) , it won't work. Because there is no function named v in the functions' namespace(see this).
So, what to do?
A probable solution can be explicit checking for the value that is to be bound to a variable. If the value is a function, then it should be bound to the variable's function value. This checking can be done via fboundp.
So, we can make a macro functioner and a modified version of match-if. functioner checks if the value is a function, and sets it aptly. match-if does the dynamic local bindings, and allows other code in the scope of the bound variables.
(defmacro functioner (var val)
`(if (and (symbolp ',val)
(fboundp ',val))
(setf (symbol-function ',var) #',val)
(setf ,var ,val)))
(defun match-if (pattern input bindings)
(eval `(and (let ,(mapcar #'(lambda (x) (list (car x))) bindings)
(declare (special ,# (mapcar #'car bindings)))
(loop for i in ',bindings
do (eval `(functioner ,(first i) ,(rest i))))
(eval (second (first ',pattern))))
(pat-match (rest ',pattern) ',input ',bindings))))

In Elisp how to dynamically assign value to a variable which also selected dynamically (in let form.)

I have a alist of known variables and corresponding functions with nil parameter list, inside body it use that known non-parameter (or global) variable.
for e.g.
(defun funA ()
(message "%s" varA))
(defun funB ()
(message "%s" varB))
...
(setq alist
'((varA . funA)
(varB . funB)
...
))
Similar element in alist can be added / deleted dynamically.
I want to run all these function in another function where the value of known variable assigned dynamically in LET form.
(defun call-each-fun-of-alist ()
(dolist (e alist)
(let (( (car e) (read-from-minibuffer "value: ") ))
(funcall (cdr e)))))
(Yes it will throw error, but I wanted to similar thing, possible without EVAL)
For known element of alist (like first I could do
(let ((varA (read-from-minibuffer "value: ")))
(funcall (cdr (assoc 'varA alist))))
But alist is dynamically updated and I what to run all functions in alist
and the value for corresponding variable will come dynamically.
Please let me know how I can define
call-each-fun-of-alist
(not necessarily but without calling EVAL inside call-each-fun-of-alist, if not possible without EVAL than I like to know it also.)
You could do this with letf (cl-letf in recent Emacs). It binds values like let but allows 'places' or 'generalized variables' as well as simple variable names.
(defun call-each-fun-of-alist ()
(cl-dolist (e alist)
(cl-destructuring-bind (variable . function) e
(cl-letf (((symbol-value variable)
(read-from-minibuffer
(format "value for %s: "
variable))))
(funcall function)))))
Note that this will fail with an error unless the variables named in alist have previously been declared as dynamic variables using defvar. Look up 'generalized variables' in the Elisp manual for more information.
Another solution would be to use cl-progv, which takes parallel lists of variable names and values to bind dynamically:
(defun call-each-fun-of-alist ()
(cl-dolist (e alist)
(cl-destructuring-bind (variable . function) e
(cl-progv
(list variable)
(list (read-from-minibuffer
(format "value for %s: "
variable)))
(funcall function)))))
In Common Lisp this is provided by PROGV - dynamic binding of a variable given the symbol of the variable.
GNU Emacs should have PROGV in its Common Lisp emulation.
This is all you need -- no need for progv or destructing bind stuff:
(defun call-each-fun-of-alist (alist)
(dolist (e alist)
(set (car e) (read-from-minibuffer "value: "))
(funcall (cdr e))))
(defvar my-alist '((varA . funA) (varB . funB)))
(call-each-fun-of-alist my-alist)
Or if you really want to see a let binding for some reason:
(defun call-each-fun-of-alist (alist)
(dolist (e alist)
(eval `(let ((,(car e) (read-from-minibuffer "value: ")))
(funcall (cdr e))))))

Common Lisp: non-nil arguments and their names to alist, how?

I am quite new to Common Lisp and programming, and I'm trying to write a certain function that turns all non-nil args into an alist. The only way I can think of so far is:
(let ((temp nil))
(if arg1
(setf temp (acons 'arg1 arg1 nil)))
(if arg2
(setf temp (acons 'arg2 arg2 temp)))
...
(if arg20-ish
(setf temp (acons 'arg20-ish arg20-ish temp)))
(do-something-with temp))
which does not seem very elegant, it would be messy with many arguments and when these need to be changed. I am looking for a smarter way to do this, both for the sake of writing this particular function and for learning how to think in Lisp and/or functional programming.
The tricky part for me is figuring out how to get the names of the arguments or what symbol to use, without hand coding each case. If &rest provided arg names it would be easy to filter out NILs with loop or mapcar, but since it doesn't, I can't see how to "automate" this.
I'm totally interested in other solutions than the one described, if people think this way is unnatural.
Edit: Below is an example of what I am trying to do:
An object is created, with a non-fixed number of data pairs and some tags, e.g.:
user = "someone"
creation-time = (get-universal-time)
color-of-sky = "blue"
temperature-in-celsius = 32
language = "Common Lisp"
...
tags = '("one" "two" "three")
These properties (i.e. key/arg names) could be different each time. The new object will then be added to a collection; I thought the array might work well since I want constant access time and only need a numeric ID.
The collection will hold more and more such custom objects, indefinitely.
I want to be able to quickly access all objects matching any combination of any of the tags used in these objects.
Since the array is supposed to store more and more data over a long period, I don't want to parse every item in it each time I need to search for a tag. Thus I also store the index of each object with a given tag in a hash-table, under the tag name. I have written this function, what I find difficult is figuring out how to collect the data and turn it into an alist or anything that I can easily parse, index, and store.
This macro will define a function that turns its non-nil arguments into an alist bound during execution of the body:
(defmacro defnamed (fun-name alist-sym (&rest args) &body body)
`(defun ,fun-name (,#args)
(let ((,alist-sym))
,#(mapcar
(lambda (s)
`(when ,s
(push (cons ',s ,s) ,alist-sym)))
(reverse args))
,#body)))
Demonstration:
(defnamed make-my alist (a b c)
alist)
(make-my 1 NIL 3)
=> ((A . 1) (C . 3))
Here's a sort of solution using macros:
(defmacro named-args (fun-name alist-sym (&rest syms) &body body)
`(defun ,fun-name (&key ,#syms)
(declare (special ,#syms))
(let ((,alist-sym
(loop
for s in ',syms
collecting (cons s (symbol-value s)))))
,#body)))
You can then use it with something like
(named-args f u (a b c)
(format t "~A~%" u))
which expands to
(DEFUN F (&KEY A B C)
(DECLARE (SPECIAL A B C))
(LET ((U
(LOOP FOR S IN '(A B C)
COLLECTING (CONS S (SYMBOL-VALUE S)))))
(FORMAT T "~A~%" U)))
Finally, calling will give
(f :a 3) => ((A . 3) (B) (C))
Note that we need the special declaration otherwise symbol-value doesn't work (you need a global binding for symbol-value). I couldn't find a way to get rid of that.
Looking at your question again, it looks like you actually don't want the keyword arguments that didn't get passed. In which case you could parse a &rest argument (although that's a flat list, so you'd need to map along it in twos) or you could modify the macro as follows:
(defmacro named-args (fun-name alist-sym (&rest syms) &body body)
`(defun ,fun-name (&key ,#syms)
(declare (special ,#syms))
(let ((,alist-sym
(loop
for s in ',syms
when (symbol-value s)
collecting (cons s (symbol-value s)))))
,#body)))
and then you get
(f :a 3) => ((A . 3))