LISP function substitute does not substitute my string [closed] - lisp

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to understand the function "substitute" in LISP, but there is something that I don't understand.
When I perform this:
(defparameter *mytree* nil)
(push 1 *mytree*)
(substitute '8 '1 *mytree*)
Everything runs ok, the value of mytree is (8).
But, when I perform:
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute '8 '"A" *mytree*)
Then mytree is ("A") instead of (8) like I expect.
Any idea why this is happening?

You need to use the correct equality checking method by using the :test keyword in substitute.
The signature for substitute is
(substitute newitem olditem sequence &key from-end test test-not start end count key)
Substitute is using eql by default, but that won't work in this scenario
(eql "A" "A") ;; nil
(equal "A" "A") ;; t
This will yield the correct results
(defparameter *mytree* nil)
(push "A" *mytree*)
(substitute 8 "A" *mytree* :test 'equal) ;; note the test, returns (8)

Related

Adapting UCI lisp function with a loop to common lisp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to recreate an old program written in UCI Lisp using Common lisp but I'm not very fluent with Lisp.
The original function is:
(DE SETROLE (ROLE FILLER CD)
(CONS (HEADER:CD CD)
(APPEND (FOR (PAIR IN (ROLES:CD CD))
(WHEN (NOT (EQUAL (ROLE:PAIR PAIR) ROLE)))
(SAVE PAIR))
(LIST (LIST ROLE FILLER]
Here's my common lisp interpretation:
(defun setrole (role filler cd)
(cons (header/cd cd)
(append (loop for pair in (roles/cd cd)
do (when (not (equal (role/pair pair) role))
(save pair)))
(list (list role filler)))))
This is the error that comes up:
*** - EVAL: variable WHEN has no value
My initial thinking is that there is no equivalent 'save' function. The description of the function is:
SETROLE Makes a new CD form with (ROLE FILLER) added or replacing the old (ROLE ...) pair.
Kindly help, I'm stumped
I found the bug. There was a loose 'when' in the wild below the code.

Difference in function evaluation between Scheme and Elisp [duplicate]

This question already has answers here:
What is the difference between Lisp-1 and Lisp-2?
(2 answers)
Closed 3 years ago.
While studying elisp I tried something which I know works in Scheme and discovered to my surprise that I couldn't replicate it in Elisp.
;; works in Scheme. result: 5
((if 1 + -) 3 2)
;; doesn't work in Elisp. result: error
((if 1 '+ '-) 3 2)
I would expect that the Elisp line evaluates to
(+ 3 2)
and that the evaluation of this list result in 5. However I get:
(invalid-function (if t '+ '-))
What am I missing here? Does Elisp not allow for such actions? Is this possibly related to the fact that Scheme is a lisp-1 language and Elisp a lisp-2?
Yes, the error is due to the fact that Emacs Lisp is a Lisp-2. This should work:
(funcall (if 1 '+ '-) 3 2)

How to interpret a list parameter as an actual list, not a symbol? [duplicate]

This question already has answers here:
Resolving symbols in a Common Lisp list
(2 answers)
Closed 6 years ago.
I have a situation like this in my code:
(defparameter names (list "Alice" "John" "Jack"))
(defparameter pair '(:smt names))
(defun process (a-list) (first a-list))
Now (process names) is working fine but (process (second pair)) returns an error (Cannot take CAR of NAMES). When I debug, I noticed that for the latter call the parameter is regarded as a symbol by type-of, not a values list (listp returns nil). How can I overcome this or what am I doing wrong? Isn't (second pair) the same thing as names?
It is very easy to check whether (second pair) is the same as names in your REPL.
CL-USER> (second pair)
NAMES
CL-USER> names
("Alice" "John" "Jack")
CL-USER> 'names
NAMES
CL-USER> (second (list ':smt names))
("Alice" "John" "Jack")
CL-USER> (second (list ':smt 'names))
NAMES
You can see that it's the same as 'names; pair is a list of two symbols.
It's possible that you want to use quasiquote and evaluate names instead of quoting it:
CL-USER> (second `(:smt ,names))
("Alice" "John" "Jack")

Increment variable lisp in local function [duplicate]

This question already has answers here:
Variable references in lisp
(7 answers)
Closed 8 years ago.
How can i increment a variable in a local function and the change to persist ?
(defun inc(x) (+ x 1))
(setq a 1)
I want this (inc a) to change the value of a to 2
I searched this problem , and i find something with setf but i couldn't resolve the problem.
Depending on your exact problem, this may be answered in Variable references in lisp, which discusses the particular issues of lexical scope that you may be encountering. It even uses the same example of incrementing the value of a variable, so it's particularly relevant.
However, if you're just looking for how to increment a value, then you may be interested in the standard function 1+, where (1+ x) is equivalent to (+ x 1), and the macro incf for incrementing the value of a place by 1.
(let ((a 1)) (let ((a 1))
(1+ a)) (incf a)
a)
;=> 2 ;=> 2
However, depending on exactly what you're trying to do, these might not work for you (and the possible duplicate is more appropriate). If you're trying to call a function with the value of a variable, and then have the body of the function effect a change in the binding of the variable, then you can't do it. That is, there is no function that can make this work:
(let ((a 1))
(foo a)
a)
;=> 2
The function foo is called with the value of the variable a, which is the number 1; it doesn't have any access to the variable, so it can't bind the variable to a new value. However, a macro can work with the unevaluated forms it is called with, so you can have the sort of side effects that you may be looking for. E.g.:
(let ((a 1))
(incf a)
a)
;=> 2
However, as explained above, you can't do:
(defun my-incf (x)
(incf x)) ; modifies the binding of x
(let ((a 1))
(my-incf a)
a)
;=> 2
because the call to incf changes the binding of x, which is a variable that is local to my-incf.

Quotation mark in lisp code macro output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i get something like:
(my-macro name) => (foo "foo-transformed-arg-name")
I only obtained
(foo \#" foo-transformed-arg-name \#")
How can i avoid the #" in my macro output?
(defmacro foo (sym)
(symbol-name sym))
or
(defmacro foo (sym)
(string-downcase (symbol-name sym)))
I don't get why you would need any macro like this, but you will know best.
This is probably what you want to do:
(defmacro static-string (func &rest characters)
`(,func ,(coerce characters 'string)))
Perhaps it may make sense if you need to define some string constants instead of creating them at run time...
And this is the only way I can think of you being able to arrive at your current result:
(defmacro quote-symbol-quote (func symbol)
(list func #\" symbol #\"))
Stackoverflow highlighting is misleading. What you are getting in the result is a function called with three arguments: the quote character, the symbol foo-transformed-arg-name and the quote character again.
The resolution was very simple. My real probem was how can I convert mi argument symbol of a macro to an string.
(my-macro foo) => (other-func "foo")
That is convert the sybol foo to "FOO".
For that i used the symbol-name function