Re: Delaying evaluation of operands ...



"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.
.



Relevant Pages

  • Re: What is the main advantage of macros over functions?
    ... if one is allergic to reading Lambda one may better not use ... Lisp authors, doesn't help you much, I'd say. ... >> Readmacros are a completely different mechanism. ... Macros and Readmacros ...
    (comp.lang.lisp)
  • Re: define-syntax how-to question
    ... lambda list for the final lambda form. ... much more difficult to deal with the wrapped syntax objects that you ... r5rs and define-syntax, ... let extensions provide their own define-xyz-method macros. ...
    (comp.lang.scheme)
  • Re: define-syntax how-to question
    ... where some-stuff is the output of a user-supplied function that takes ... lambda list for the final lambda form. ... much more difficult to deal with the wrapped syntax objects that you ... let extensions provide their own define-xyz-method macros. ...
    (comp.lang.scheme)
  • Re: Macros still needed with shorter LAMBDA?
    ... > want to keep typing LAMBDA. ... I also use macros so I do not have to ... I do not mind typing ', but where an embedded language is ... signature (it is an attribute of the Cells engine, ...
    (comp.lang.lisp)
  • Re: Object Copying
    ... collapse the difference between read-time, compile-time, and run-time into ... principally it happens because macros are traditionally ... lambda, let*, and so forth) give you that power, why collapse them into ...
    (rec.games.roguelike.development)