# Lisp question

This topic is 4798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I don't know if anyone here works of understands lisp, but as lisp is used in articial inteligence, i tought in giving it a shot here. I started working with lisp not a long time ago, but i have to say, its the weirdest language i ever seen. :) Anyways, to the question.., I'm doing a program that has to count how many books a person picks up from a library. Each person has a certain number of books that can be picked up. When a person picks up a book, i subtract one value from the books she is allowed to pickup, and if the number is zero, i don't let it pick up the book. All is fine, but when i exit the program, it crashes, crashes badly, and deletes all the content i had in the lists. The code that does the subtract is the following : (defun add_book(caminho area) (let ((resp (second area))) (cond ((<= resp 0) (format t "maximum numbers of books already picked up") (main-menu)) ((> resp 0) (cons area caminho) (- (second area)1)) ))) If i comment this line (- (second area)1)) Obvioulsy the books are not subtracted anymore, but the program won't crash ??? Am i doing something wrong here ? Btw, i'm working with Allegro Common lisp. thanks for any help, Bruno

##### Share on other sites
Sounds like something weird is going on. I may be wrong, but it was my understanding that under normal circumstances a lisp interpreter shouldn't crash, at least not the way a c program does. Is it giving you an error message or stack trace? One thing I noticed is your (cons area caminho) statement doesn't appear to be doing anything. Again, my lisp may be rusty but it looks like you might want to be changing something there with set unless you are rebuilding the structure and returning it. Maybe (set caminho (cons area caminho))?

##### Share on other sites
When i mean crash, i don't mean crash like a C program.
It's a error inside the lisp interpeter.
The error is the following :

Error: Attempt to take the car of 0 which is not listp.
[condition type: SIMPLE-ERROR]

The problem is not in (cons area caminho) , the structure is being filled correctly with the data i'm feeding to it, and the subtraction is also happening, the problem is on exit, gives me the error i posted above, and deletes the data content that i had inserted.
Speaking from the point of view of C , it seems to be that somehow i'm overflowing the lists by performing that subtraction..

##### Share on other sites
Lisp is not weird, it just smells funny.

You didn't post enough code for me to replicate your error. At the very least, you should add the call to the function that creates the error, the values of any variable you use when calling the function, and what you want your function to return / what side effects you want the function to have. You can also (trace add-book) before calling it and post here the results - there's also (untrace add-book).

Looking at your code, apart from the lack of proper indenting style, I can see that (cons area caminho) does nothing. It will create a pair of area and caminho (if caminho is a list (cons area caminho) is a new list with the area element added in the first place) but that pair is thrown away: cons does not modify the variables it receives - it just returns a new pair - and you do nothing with the returned value.

Also, (- (second area) 1) will return a value from your function, but it will not modify the second element of area (decf (second area)) will do that.

##### Share on other sites
Smells very funny :P eeh
Thanks for the explanation,
Here it goes :

(menu-principal) its just a menu, where we can select what to do.
When i choose to withdraw a book, it comes to this function :

(defun assign_book_client(carro)
(dolist (area *area*)
(format t "~2%~2tBook name: ~5A" (nome_area area))
(format t "~2%~2tNº Maximum number of books that can be withdrawn: ~5D" (num_carros area))
(format t "~2%~2t1 - Assign this book to this client ?")
(format t "~2%~2t2 - Next book~2%")
(let ((resp_area (lenumero 1 2)))
(if (= resp_area 1)

function return_book

(defun return book (carro)
(third carro))

Also, replacing (- (second area) 1) with (decf (second area)) also decreases the number of the books, but still gives that error on exit, but, doens't delete the content of the lists anymore.

##### Share on other sites
Can you please (trace add_book), (trace assign-book-client) and (trace return_book) and then run the program again so you get the error. Please post the output of trace so we can see the arguments add_book is called with before it fails.

[Edited by - Diodor on June 15, 2005 7:44:55 AM]

##### Share on other sites
I wasn't familiar with the command trace, so i browsed around and i found its used in the lisp interpreter, and when i execute it in the lisp interpreter, it gives me this :

Error: Attempt to take the value of the unbound variable TRACE'.
[condition type: UNBOUND-VARIABLE]

It seems he assumes Trace as being a variable ?
Or am i using it wrong ?

##### Share on other sites
Quote:
 Original post by BrunoI wasn't familiar with the command trace, so i browsed around and i found its used in the lisp interpreter, and when i execute it in the lisp interpreter, it gives me this :Error: Attempt to take the value of the unbound variable TRACE'.[condition type: UNBOUND-VARIABLE]It seems he assumes Trace as being a variable ?Or am i using it wrong ?

Possibly you are using it wrong (as a variable not as a function), but I can't tell because you didn't say what command you entered in the interpreter. Did you (trace function_name) ?

trace is in the hyperspec, so it should be defined.

##### Share on other sites
Ok, i was using the trace function wrong.
I figure it out how to use it.

Cobol, Richard -> Its a book name, with Richard being the author
Bruno,3 -> Its the client and the number of books he can widthraw

This is the output :

0[1]: (ASSIGN-BOOK-CLIENT (COBOL RICHARD NIL))

Client name: BRUNO

Maximum number of books: 3

1 - Select this client

2 - Next client

1[1]: (RETURN_BOOK (COBOL RICHARD NIL))
1[1]: returned NIL
1[1]: (ADD_BOOK NIL (BRUNO 3 NIL NIL))
1[1]: returned 2

Thats after i assign a book to a client (ignore the portuguese language in the menu)
Then, when i exit the program, it gives me this :

| Bem vindo a nossa oficina |
| Por favor escolha a operação pretendida: |
|_____________________________________________|
| 0 - Terminar programa |
| 1 - Definir areas |
| 2 - Entrar viatura (Diagnostico) |
| 3 - Alterar percurso de viatura |
| 4 - Finalizar viatura numa area |
| 5 - Listagem de areas |
|_____________________________________________|
Introduza a opção pretendida: 0

0[1]: returned NIL
0[1]: (RETURN_BOOK (COBOL RICHARD 2))
0[1]: returned 2
Error: Attempt to take the car of 2 which is not listp.
[condition type: SIMPLE-ERROR]
CG-USER(11):

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004466

×