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

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.

Related

Clisp "Program stack overflow. RESET" on a (cadr). How? [duplicate]

This question already has an answer here:
Unusual stack overflow when inserting nodes in binary tree
(1 answer)
Closed 6 years ago.
I'm (still) porting code from Franz Lisp to Common LISP. Now I seem to have pushed the interpreter into a strange corner where it blows up.
[11]> (setq fff (cadr contextstack))
*** - Program stack overflow. RESET
Now how can that cause a stack overflow? Where's the recursion?
I can take the length OK:
[12]> (length contextstack)
79
describe works. This is just the beginning of long "describe" output.
[13]> (describe (cadr contextstack))
(# . #1=#) is a cons.
#<FUNCTION POPPATTERN (NODE) (DECLARE (SYSTEM::IN-DEFUN POPPATTERN)) (BLOCK POPPATTERN (MAPONE # #) (XEPATTERN NODE #))> is an interpreted
function.
Argument list: (NODE)
#1=# is a structure of type ENODE.
Slots:
EROOT =
#1=#S(ENODE :EROOT #1# :EQCLASS #1# :ESUCCESSORS ALLTRUE! :ESLENGTH NIL :EPREDECESSORS NIL :ECONGRUENT NIL :EDEMON NIL :EMERGEDEMON NIL
:EPATTERN
;;; much more
But almost anything else applied to contextstack blows up with the stack overflow message.
The structure ENODE contains links to other ENODE items, and there is circularity which could make some code loop. But cadr? How is that
even possible?
The original code used "hunks", a MacLISP feature from the 1970s. I converted it to use Common LISP structs, and may have broken something.
This is all running interpretive; I haven't compiled anything. Safety level is the default. I tried adding (proclaim '(optimize (safety 3))) to force more checking, but it didn't change anything.
If you want to reproduce this, download the code in
https://github.com/John-Nagle/pasv/tree/master/src/CPC4
start "clisp", do (load 'setup), and then start looking at contextstack. Whatever is breaking is happening early, as the program is initializing.
How you got the error
Chances are, the error is in printing, not cadr. You can verify it by doing
(progn (cadr contextstack) nil)
which should work just fine.
If it still blows up, you should check if cadr raises an exception (and then the error message would contain a circular object and blow up):
(and (consp contextstack)
(consp (cdr contextstack)))
How to avoid the error
CLISP FAQ says:
You will always get a stack overflow when you try to print a circular object (LIST, STRUCTURE-OBJECT, VECTOR etc) and *PRINT-CIRCLE* is NIL. Just set *PRINT-CIRCLE* to T.
Note that this is not CLISP-specific, you should get the same error with any ANSI Common Lisp when *print-circle* is nil.
In your specific case, look closely at the output of (describe (cadr contextstack)):
(# . #1=#) is a cons.
This #= notation is used by the
printer to avoid stack overflows when printing circular objects.
PS. You now owe me 10 zorkmids :-)

What does the `#` mean in elisp? [duplicate]

This question already has answers here:
What is the role of the # character in Emacs Lisp?
(2 answers)
Closed 7 years ago.
Quite likely this is a dumb question, but I haven't come across the # symbol in the bits of elisp I have read, and was wondering what it means (preceded by a , as well) in this code? I have had some difficulty providing the proper search phrase I think.
In case of link rot:
(defmacro zenburn-with-color-variables (&rest body)
"`let' bind all colors defined in `zenburn-colors-alist' around BODY.
Also bind `class' to ((class color) (min-colors 89))."
(declare (indent 0))
`(let ((class '((class color) (min-colors 89)))
,#(mapcar (lambda (cons)
(list (intern (car cons)) (cdr cons)))
zenburn-colors-alist))
,#body))
This is an elisp macro definition, it defines a template for code to be substituted by other code at compile time. A decent intro is chapter 7 of Paul Graham's On Lisp
http://www.paulgraham.com/onlisptext.html
Ask Emacs, by checking the index of the elisp manual:
C-hig (elisp) RET
I # RET
Follow result: * ,# (with backquote) [Index]: Backquote. (line 29)
You can also "splice" an evaluated value into the resulting list,
using the special marker ‘,#’. The elements of the spliced list become
elements at the same level as the other elements of the resulting list.
The equivalent code without using ‘`’ is often unreadable. Here are
some examples:
[...]

LISP function substitute does not substitute my string [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
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)

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

Why do some use #'(lambda instead of just (lanbda in Common Lisp? [duplicate]

This question already has answers here:
Why use #' with lambda?
(2 answers)
Closed 9 years ago.
Why do some use #'(lambda instead of just (lambda in Common Lisp? Are there performance benefits or something?
Because, as Peter Siebel and others explain, in CL, "the following LAMBDA expression: (lambda () 42) expands into the following when it occurs in a context where it evaluated: (function (lambda () 42))".
There is not performance benefits except few milliseconds of compile time vs few millisecods of read time :)
I think real reason is consistency. If one writes (mapcar #'myfunc ...) (not just (mapcar myfunc ...)), it is natural to write (mapcar #'(lambda ...) ...) too.