Re: Stylistic question on control transfer (return vs. throw)



eric@xxxxxxxxxxxx writes:
On 06 Feb 2006 20:29:21 +0000, Alan Crowe
<alan@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

CL-USER> (cut-out '(we want to omit from here all the way to here
and skip everything between the skip
symbols)
'())
=> (WE WANT TO OMIT FROM AND SYMBOLS)

Shouldn't it omit everything between the 2 TO's?

1)How else might one code this?

(defun cut-out (list)
(when list
(let* ((a (car list))
(b (cdr list))
(c (member a b)))
(if c (cut-out (cdr c))
(cons a (cut-out b))))))

This post was a big shock to me. I had intended that my code
process the list left to right, but hadn't spotted the two
`to's in my test case, so I thought the code had passed.

As Kaz points out, its is an ebb versus flood thing.

CL-USER> (defun starts-with (tag list)
(if (eql tag (car list))
list
nil))
STARTS-WITH
CL-USER> (defun member-flood (item list)
(and list
(or (starts-with item list)
(member-flood item (cdr list)))))
MEMBER-FLOOD
CL-USER> (member-flood 3 '(1 2 3 4 5 4 3 2 1))
(3 4 5 4 3 2 1)
CL-USER> (defun member-ebb (item list)
(and list
(or (member-ebb item (cdr list))
(starts-with item list))))
MEMBER-EBB
CL-USER> (member-ebb 3 '(1 2 3 4 5 4 3 2 1))
(3 2 1)

I was imagining that THROW unwinds the stack to the tag
before evaluating the expression it contains, but infact it
works more like a function, evaluating the internal
expression first, with the result that the list is processed
on the ebb, ie backwards.

So I've learned more about catch and throw from
participating in this thread. However, the example I was so
proud of doesn't look so good any more.

Does anyone have a convincing example of catch and throw
with run time computation of tags that is an actual
algorithm and not just error handling?

Alan Crowe
Edinburgh
Scotland
.