Re: Delaying evaluation of operands ...
- From: Pascal Bourguignon <spam@xxxxxxxxxxxxxxxx>
- Date: Mon, 30 Jan 2006 02:27:08 +0100
"Jérémie Lumbroso" <jeremie.lumbroso@xxxxxxxxx> writes:
> Hello,
>
> I've noticed that while I can get the desired result with:
> (or #t (/ 0 0))
>
> I don't how duplicating that effect with my own functions, without
> using the special form "or" at some point. I tried a few attempts to
> make sure I wasn't just overcomplicating something that was naturally
> handled by Scheme:
>
>
> (define (my-or-1 test-1 test-2)
> (if (test-1)
> #t
> (if (test-2)
> #t
> #f)))
>
> (define (my-or-2 . L)
> (define (internal-or L)
> (and (pair? L)
> (if (car L)
> #t
> (internal-or (cdr L)))))
> (internal-or L))
>
>
> Both complain about the divide by zero.
>
> How can I handle this transparently (i.e. without delaying the operands
> manually, or otherwise quoting them)?
You can do it with lambda:
(define (my-or . arguments)
(cond ((null? arguments) #t)
(((car arguments)))
(else (apply my-or (cdr arguments)))))
(my-or (lambda () #t) (lambda () (/ 1 0))) --> #t
(my-or (lambda () #f) (lambda () 2) (lambda () #t)) --> 2
(my-or (lambda () #f) (lambda () '()) (lambda () #t)) --> ()
Macros are but syntactic sugar for lambda ;-) But it helps a lot to
implement syntactic abstraction, and this is essential.
You need macros (syntax-case works too) to implement new special
operators in lisp!
--
__Pascal Bourguignon__ http://www.informatimago.com/
In deep sleep hear sound,
Cat vomit hairball somewhere.
Will find in morning.
.
- Follow-Ups:
- Re: Delaying evaluation of operands ...
- From: Jérémie Lumbroso
- Re: Delaying evaluation of operands ...
- References:
- Delaying evaluation of operands ...
- From: Jérémie Lumbroso
- Delaying evaluation of operands ...
- Prev by Date: Delaying evaluation of operands ...
- Next by Date: Re: Delaying evaluation of operands ...
- Previous by thread: Delaying evaluation of operands ...
- Next by thread: Re: Delaying evaluation of operands ...
- Index(es):
Relevant Pages
|