Re: coerce for arbitrary types
- From: Alan Crowe <alan@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 12 Apr 2008 19:26:17 +0100
lisp1.3.CalRobert@xxxxxxxxxxxxxxx (Robert Maas, http://tinyurl.com/uh3t) writes:
Regarding that proposed koan earlier: Does anybody have aFrom: vanekl <va...@xxxxxxx>
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?
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
.
- Follow-Ups:
- Short koan-like code snippets (was: coerce for arbitrary types)
- From: Robert Maas, http://tinyurl.com/uh3t
- Re: coerce for arbitrary types
- From: Kent M Pitman
- Short koan-like code snippets (was: coerce for arbitrary types)
- References:
- coerce for arbitrary types
- From: Albert Krewinkel
- Re: coerce for arbitrary types
- From: Scott Burson
- Re: coerce for arbitrary types
- From: Robert Maas, see http://tinyurl.com/uh3t
- Re: coerce for arbitrary types
- From: Kent M Pitman
- Re: coerce for arbitrary types
- From: Robert Maas, see http://tinyurl.com/uh3t
- Re: coerce for arbitrary types
- From: vanekl
- Re: coerce for arbitrary types
- From: Robert Maas, http://tinyurl.com/uh3t
- coerce for arbitrary types
- Prev by Date: Re: Oft-shared (perhaps?) Impressions of a Lisp Newbie
- Next by Date: Re: Oft-shared (perhaps?) Impressions of a Lisp Newbie
- Previous by thread: How best to teach newbie at algorithms (was: coerce for arbitrary types)
- Next by thread: Re: coerce for arbitrary types
- Index(es):
Relevant Pages
|