From: Michael Anthony Villafana
Subject: re: do loop question...
Date: 
Message-ID: <4gdemr$ec7@ring24.cs.utsa.edu>
Why will this code segment crash?

.
.
.
(do ((var1 val1 (cdr ll)) (sum 0))  ; I don't use sum, just declare it.
       ((null l) 'some-message)
       ((member c (car ll)) (car ll)))
.
.
.
...the problem I am having is that the do-loop crashes when it 
gets to the 'member' function.  The error message is: 
Unknown operator (MEMBER C (CAR LL)) in (FUNCTION (MEMBER C (CAR LL)))
Thanx...
·······@runner.cs.utsa.edu

From: Donald Fisk
Subject: Re: do loop question...
Date: 
Message-ID: <4gfkr1$56v@pheidippides.axion.bt.co.uk>
Michael Anthony Villafana (········@ringer.cs.utsa.edu) wrote:
: Why will this code segment crash?

: .
: .
: .
: (do ((var1 val1 (cdr ll)) (sum 0))  ; I don't use sum, just declare it.
:        ((null l) 'some-message)
:        ((member c (car ll)) (car ll)))

It's hard to know where to begin.   Firstly, l is underined.   Presumably
you mean ll, which is also undefined.   Presumably var1 should be replaced
by ll.   Presumably c is defined outside the code.

Secondly, it you intend the loop to have two terminating
conditions, you can't do that: do permits only one.   You can write

(do ((ll val1 (cdr ll)) (sum 0))
    ((or (null ll) (member c (car ll)))
     (if (null ll) 'some-message (car ll))))

instead, which is what I think you want.

This would make sense if val1 originally contained a list of lists,
at least one of which might have contained whatever the value of c is.

E.g. if c has the value of 'c, and val1 has the value of '((a b) (c d) (e f)),
your expression would return (c d).   If c had the value of 'k, it would
return some-message instead.

Your Lisp thought that ((member c (car ll)) (car ll)) was part of the
body of the do loop, to be executed once each time round the loop.   It
complained because it expected a function name and got
((member c (car ll)) (car ll)) instead.

By the way, it's considered bad style (by me, at least) to use car
and cdr on lists.   first and rest are preferred.   You might also
want to use endp instead of null.   endp gives an error if it doesn't
get a list.   car and cdr are better reserved for dotted pairs.

: ...the problem I am having is that the do-loop crashes when it 
: gets to the 'member' function.  The error message is: 
: Unknown operator (MEMBER C (CAR LL)) in (FUNCTION (MEMBER C (CAR LL)))
: Thanx...
: ·······@runner.cs.utsa.edu

--
Le Hibou (mo bheachd fhe/in:my own opinion) Email: ······@info.bt.co.uk
"[British] lager is an imitation continental beer drunk only by refined
ladies, people with digestive ailments, tourists, and other weaklings."
			-- Muenchen Suddeutsche Zeitung, April 1976.
From: Donald Fisk
Subject: Re: do loop question...
Date: 
Message-ID: <4ghff6$qkk@pheidippides.axion.bt.co.uk>
Donald Fisk (······@srd.bt.co.uk) wrote:
: Michael Anthony Villafana (········@ringer.cs.utsa.edu) wrote:
: : Why will this code segment crash?
: : (do ((var1 val1 (cdr ll)) (sum 0))  ; I don't use sum, just declare it.
: :        ((null l) 'some-message)
: :        ((member c (car ll)) (car ll)))

: It's hard to know where to begin.   Firstly, l is underined.   Presumably
: you mean ll, which is also undefined.   Presumably var1 should be replaced
: by ll.   Presumably c is defined outside the code.

: Secondly, it you intend the loop to have two terminating
: conditions, you can't do that: do permits only one.   You can write

: (do ((ll val1 (cdr ll)) (sum 0))
:     ((or (null ll) (member c (car ll)))
:      (if (null ll) 'some-message (car ll))))

: instead, which is what I think you want.

: This would make sense if val1 originally contained a list of lists,
: at least one of which might have contained whatever the value of c is.

: E.g. if c has the value of 'c, and val1 has the value of '((a b) (c d) (e f)),
: your expression would return (c d).   If c had the value of 'k, it would
: return some-message instead.

Better still, you can get rid of the do loop altogether by replacing
it with find-if:

(or (find-if #'(lambda (x) (member c x)) val1) 'some-message)

I think that may be the most elegant solution.

--
Le Hibou (mo bheachd fhe/in:my own opinion) Email: ······@info.bt.co.uk
"[British] lager is an imitation continental beer drunk only by refined
ladies, people with digestive ailments, tourists, and other weaklings."
			-- Muenchen Suddeutsche Zeitung, April 1976.