Re: coerce for arbitrary types



lisp1.3.CalRobert@xxxxxxxxxxxxxxx (Robert Maas, http://tinyurl.com/uh3t) writes:

Regarding that proposed koan earlier: Does anybody have a
collection of short code snippets which likewise illustrate a whole
mess of deep understanding, where just trying to understand why
this particular snippet of code produces the result it does will
enlighten a newbie?
From: vanekl <va...@xxxxxxx>
a 10-line recursive BFS

I assume you mean breadth-first search (in some sort of tree,
possibly binary)? Why would anybody want to write that recursively?

For fun :-)

Let me see if I can find the 10-line recursive BFS algorithm ...

Will four lines do?

(defun bfs6 (test children pending)
(and pending
(or (some test pending)
(bfs6 test children (mapcan children pending)))))

I've forgotten which Pascal showed me this, sorry.
It fails to return the path.

CL-USER> (loop for i from 10 to 20
do (print (bfs6 (equal-to i)
(lambda(x)(remove 1000
(list (* x 2)
(* x 3)
(* x 5))
:test #'< ))
(list 1))))

T
NIL
T
NIL
NIL
T
T
NIL
T
NIL
T

I cannot remember the right way to fix this, but ruthless
cheating is fun too:


CL-USER> (defun test-path (test)
(lambda(path)
(if (funcall test (car path))
path
nil)))

CL-USER> (defun child-path (children)
(lambda(path)
(loop for child in (funcall children (car path))
collect (cons child path))))

CL-USER> (loop for i from 10 to 20
do (print (bfs6 (test-path (equal-to i))
(child-path (lambda(x)(remove 1000
(list (* x 2)
(* x 3)
(* x 5))
:test #'< )))
(list (list 1)))))

(10 2 1)
NIL
(12 4 2 1)
NIL
NIL
(15 3 1)
(16 8 4 2 1)
NIL
(18 6 2 1)
NIL
(20 4 2 1)


Alan Crowe
Edinburgh
Scotland
.



Relevant Pages