Reputation: 10830
Note: First time using lisp*
I have a homework, and we are suppose to essentially bind expressions with either a 0 or 1. Example:
(defun orexp (a b) (list 'or a b))
(setq p3 (orexp 1 'a)) ;which equals (or 1 a)
Say I wanted to create a function to evaluate them by binding them and simplify them, but ignoring the simplification, how can I bind them doing something like this:
(evalexp p3 '((a 0)))
and end up with:
(or 1 0)
I tried searching it, but I can't find anything. Please let me know if it needs a better explanation, but I am leaving information out because its a homework and I do not want answers, hints on how to go about my problem. Thanks.
Upvotes: 1
Views: 236
Reputation: 769
Actually, eq is implementation-dependent on its specificity isn't it?:
The effect is that Common Lisp makes no guarantee that eq is true even when both its arguments are ``the same thing'' if that thing is a character or number. (from the hyperspec)
So eq should work if you quote the arguments, involved, but I think you could write a small little macro to do what you want without hand-quoting if you wanted:
(defmacro es (x y)
`(eq (quote ,x) (quote ,y)))
works I think.
Upvotes: 0
Reputation: 51501
You can compare symbols with eq
.
Your function is something like (defun evalexp (expression bindings) #| ... |#)
, where
the commented part (#| ... |#
) would contain your code. It would have to walk the expression
tree, and for each symbol it finds, check whether a binding of that symbol exists in bindings
, replacing the symbol with the value when that is the case.
Upvotes: 1
Reputation: 5650
In the title, you say you want to compare symbols (what you call variable names).
> (eq 'a 'a)
true
> (eq 'a 'b)
false
Is that what you mean?
Upvotes: 2