Re: AIM-8
- From: Pascal Bourguignon <spam@xxxxxxxxxxxxxxxx>
- Date: Wed, 30 Nov 2005 07:58:26 +0100
nallen05@xxxxxxxxx writes:
> Hello,
>
> This is actually meant to be a post to the following thread
>
>
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/76b970c391499b3/36f2bf9aa3cea3cd?q=AIM-8&rnum=1#36f2bf9aa3cea3cd
>
> but google groups wouldn't let me post to it, I guess it's to old or
> something...
>
> Anyways, someone mentioned Pascal's AIM-8 in cl implementation last
> night in #lisp. I tried to to load it but there's some function
> HANDLING-ERRORS in the REPL defun that isn't defined in either of the
> documents or included in any cl implementations I know of.
Sorry. Here it is:
(defmacro handling-errors (&body body)
`(HANDLER-CASE (progn ,@body)
(simple-condition
(ERR)
(format *error-output* "~&~A: ~%" (class-name (class-of err)))
(apply (function format) *error-output*
(simple-condition-format-control err)
(simple-condition-format-arguments err))
(format *error-output* "~&"))
(condition
(ERR)
(format *error-output* "~&~A: ~% ~S~%" (class-name (class-of err)) err))))
> My question is about the primitive LABEL, I couldn't quite figure it
> out from the code. The only place it's used in the document is is in
> the comment
>
> ;;(funcall (LABEL fact (lambda (x) (cond ((= 1 x) 1) (t (* x (fact (1-
> x))))))) 6)
>
> Thus I'm guessing that it is in fact the parent of common-lisp's
> #'labels.
They're related. LABEL is used to give a name to a lambda, to allow it
to recursively call itself. It's a "named lambda".
(LABEL fact (lambda (x) (cond ((= 1 x) 1) (t (* x (fact (1- x)))))))
<->
(LABELS ((fact (x) (cond ((= 1 x) 1) (t (* x (fact (1- x)))))))
(function fact))
((LABEL fact (lambda (x) (cond ((= 1 x) 1) (t (* x (fact (1- x)))))))
n)
<->
(LABELS ((fact (x) (cond ((= 1 x) 1) (t (* x (fact (1- x)))))))
(fact n))
(AIM-8 is a lisp-1)
In CL:LABELS you can define several inter-recursive functions, and
they're not anonymous: they're named, lexically scoped.
> Is DEFINE inadequate for representing recursive functions? Is
> this because the AIM-8 repl uses substitution and not binding?
No, DEFINE can be used too:
(define insert
(lambda (item list)
(cond ((null list) (combine item nil))
(t (combine item
(combine (first list)
(insert item (rest list))))))))
INSERT
AIM-8> (insert 'sep '(a b c))
(SEP A SEP B SEP C SEP)
AIM-8>
and for now, it's the only one that works. There are bugs in my AIM-8
implementation, the following should work as well:
((label insert
(lambda (item list)
(cond ((null list) (combine item nil))
(t (combine item
(combine (first list)
(insert item (rest list))))))))
'sep '(a b c))
but it loops infinitely; it seems the new parameters to the recursive
call don't get bound.
Note that label as lambda are special, you can only use them in definitions:
(define var (label ...))
or in function calls:
((label ...) ...)
Note: even keywords must be quoted in AIM-8!
--
__Pascal Bourguignon__ http://www.informatimago.com/
You never feed me.
Perhaps I'll sleep on your face.
That will sure show you.
.
- References:
- AIM-8
- From: nallen05
- AIM-8
- Prev by Date: Re: Cliki.net a list of packages? Was: Reddit Guys on the Pros and Cons of Lisp
- Next by Date: Re: OT to the extreme
- Previous by thread: AIM-8
- Index(es):