assoc error when list is nil

Started by HPW, November 23, 2003, 12:54:50 AM

Previous topic - Next topic

HPW

assoc gives an error when the assoc-list does not exist (nil).



I am not sure what is better:

Alisp give back only a nil.

CommonLisp gives also an error.



So now I have to check first that the list exists.



If I would get back nil, I would do  this:

(if (setq test(assoc value assoc_list))
    (setq newvar (nth 1 test))
)
Hans-Peter

nigelbrown

#1
assoc will work if assoc-list is the empty list () which is not the same as nil in newlisp

viz

newLISP v7.3.1 Copyright (c) 2003 Lutz Mueller. All rights reserved.



> (assoc 1 nil)



list expected in function assoc : nil



> (assoc 1 '())

nil

>

Perhaps the assoc-list's could be set to '() when set up



or if not, use (if to test for nil assoc-list



(if assoc-list (do-not-nil-stuff) (do-nil-stuff))



Regards

Nigel

HPW

#2
I only want to point out the difference to alisp.

I stuck on it when porting alisp code to newlisp.

Of cource it is not a problem to work around.
Hans-Peter

nigelbrown

#3
Apologies for misconstruing your point.



Just regards the comment that Common Lisp gives an error - the Hyperspec says of assoc :

"Exceptional Situations:



Should be prepared to signal an error of type type-error if alist is not an association list. "

But the common lisp implementations CormanLisp 2.5 and Allegro CL 6.2, for example, accept nil as an association list (probably as nil and '() are treated the same in Common Lisp).

eg

;; Corman Lisp 2.5  Copyright © 2003 Corman Technologies. All rights reserved.

;; Licensed to Nigel Brown [version 2.0].

(assoc 'r '((a . b) (c . d) (r . x) (s . y) (r . z)))

(R . X)

(assoc 'r nil)

NIL

(assoc 'r "string")

;;; An error occurred in function ASSOC:



And Allegro CL

CG-USER(1): (assoc 'r nil)

NIL

CG-USER(2): (assoc 'r "string")

Error: Illegal argument to endp: "string"

[condition type: TYPE-ERROR]

CG-USER(3):



I guess it comes down to the newlisp philosophy that nil is not '() but perhaps they should be treated the same in usage like (assoc ?

(particularly if it has complicated porting) What do you think Lutz?



I presume alisp would throw an error if assoc-list is a string rather than nil.



Regards

Nigel

Lutz

#4
I think I want to stick to treating nil and () only the same under boolean conditions (if, while, etc).



It may also be better from a practical point of view: when you try to 'assoc' on a 'nil' it means most of the time, that you didn't want it, misspelled a variable or used the wrong one. Then it is better to be reminded by an error message.



Lutz

HPW

#5
>Apologies for misconstruing your point.



Nigel, no need for apologies. I want every sort of discussion to learn more and get better on lisp. I had it tested with corman lisp and lispworks.

You tested assoc with symbol nil. When you test against a var which is nil, you get: The variable is unbound.



>I presume alisp would throw an error if assoc-list is a string rather than nil.



Yes then alisp throw also an error.





Lutz, it's no problem to stay with the current logic. As I said before, I get on it because of alisp-porting. But as we all know:



Lisp is a programmable programming language."

- John Foderaro, CACM, September 1991



(From Paul Graham's quotes. You see I listen to the same lisp-gurus as you, and I like his articels very much!)



So in my compatibility layer it gets a special function and then it works.



As always, Get the job done!!
Hans-Peter

nigelbrown

#6
Hi

regarding

>You tested assoc with symbol nil. When you test against a var which is nil,

>you get: The variable is unbound.



Assoc will do the same with nil and a variable bound to nil - the behaviour you described would occur is an unbound variable is passed to assoc -

example in Corman Lisp using the variable 'a' :

a

;;; An error occurred in function #< COMPILED-FUNCTION: #xD27570 >:

;;; Error: The variable A is unbound

...

(assoc 'r nil)

NIL

(assoc 'r a)

;;; An error occurred in function #< COMPILED-FUNCTION: #xD7D638 >:

;;; Error: The variable A is unbound

;;; Entering Corman Lisp debug loop.

...

;;; Returning to top level loop.

(setq a nil)

NIL

(assoc 'r a)

NIL



The way tha newlisp automatically binds a newly referenced variable to nil is different from common lisp that requires a setq but the newlisp action is the more appropriate one for a language more targeted at scripting.

I believe that ISLISP (eg see http://christian.jullien.free.fr/">http://christian.jullien.free.fr/ ) even requires the variable name be bound using defglobal, let, for, or let* before it can be setq'ed to - for extra safety I guess.

eg:



C:openlisp>openlisp

;; OpenLisp v7.5.0 (Build: 3110) by C. Jullien [May  6 2003 - 18:45:55]

;; Copyright (c) 1988-2003.

...

? (setq a nil)

** setq : unbound-variable : a

? (defglobal a nil)

= a

? a

= nil

? (setq a nil)

= nil

?



Does alisp automatically bind new vars to nil?

Regards

Nigel

HPW

#7
>Does alisp automatically bind new vars to nil?



Yes!



command: !a
nil


We must accept that in the Lisp-family are some dialekts which differ in details. But there is always (mostly) an easy workaround.
Hans-Peter