[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Macros versus higher order functions.

William James

6/4/2015 7:38:00 PM

Barry Margolin wrote:

> >The crux of my question is not "can lisp do this" or "haskell rules" or
> >anything like that. Simply that many of my initial thoughts on macros
> >could also be formulated as functions, and I wanted to appreciate the
> >real power and elegance of macros and the situations where one would
> >prefer them over functions.
>
> The crux of the answer is that macros don't really provide too much that
> you can't do with higher-order functions. Mostly what they provide is
> syntactic sugar, so that it's easier to read and write these higher-order
> constructs.
>
> For instance, you could define IF as:
>
> (defun if (test then-fun &optional (else-fun (lambda () nil)))
> (funcall (cond (test then-fun)
> (t else-fun))))
>
> and then it would be used as:
>
> (if (> x 100)
> (lambda () (princ "X is really big!"))
> (lambda () (princ "X isn't so big.")))
>
> Long ago most Lisp programmers decided that having lots of lambda
> expressions all over the place like this was confusing, and macros allow
> more concise syntax to be used. Haskell uses a more compact notation for
> its anonymous functions, so it may be less of an issue there. And some
> functional languages solve the problem using lazy evaluation.

Gauche Scheme:

(define (my-if test then-fun :optional (else-fun (lambda () #f)))
(if test (then-fun) (else-fun)))

(define x 0)

(my-if (zero? x)
(cut print "x is zero.")
(cut print "99 / x is " (/ 99 x)))

--
I think that we have to remember that the real activists who are doing this are
motivated not by love for humanity, not by love for America or anything else;
they are motivated by hatred for the traditional people and culture of America.
They are motivated by hatred toward the traditional people and culture of the
European countries. --- www.redicecreations.com/radio/2014/09/RIR-140929.php
1 Answer

William James

11/29/2015 1:06:00 AM

0

WJ wrote:

> Barry Margolin wrote:
>
> > > The crux of my question is not "can lisp do this" or "haskell rules" or
> > > anything like that. Simply that many of my initial thoughts on macros
> > > could also be formulated as functions, and I wanted to appreciate the
> > > real power and elegance of macros and the situations where one would
> > > prefer them over functions.
> >
> > The crux of the answer is that macros don't really provide too much that
> > you can't do with higher-order functions. Mostly what they provide is
> > syntactic sugar, so that it's easier to read and write these higher-order
> > constructs.
> >
> > For instance, you could define IF as:
> >
> > (defun if (test then-fun &optional (else-fun (lambda () nil)))
> > (funcall (cond (test then-fun)
> > (t else-fun))))
> >
> > and then it would be used as:
> >
> > (if (> x 100)
> > (lambda () (princ "X is really big!"))
> > (lambda () (princ "X isn't so big.")))
> >
> > Long ago most Lisp programmers decided that having lots of lambda
> > expressions all over the place like this was confusing, and macros allow
> > more concise syntax to be used. Haskell uses a more compact notation for
> > its anonymous functions, so it may be less of an issue there. And some
> > functional languages solve the problem using lazy evaluation.
>
> Gauche Scheme:
>
> (define (my-if test then-fun :optional (else-fun (lambda () #f)))
> (if test (then-fun) (else-fun)))
>
> (define x 0)
>
> (my-if (zero? x)
> (cut print "x is zero.")
> (cut print "99 / x is " (/ 99 x)))

Ocaml:

let my_if bool if_true if_false =
if bool then
Lazy.force if_true
else
Lazy.force if_false ;;

my_if (3>0) (lazy (9*9)) (lazy (8/0));;

===>
81

--
Jews may, as a result, perceive themselves to have a vital interest in
advocating a highly individualist, atomized gentile culture while
simultaneously maintaining their own highly elaborated collectivist subculture.
This is the perspective developed by the Frankfurt School and apparent
throughout Studies in Prejudice. --- Dr. Kevin MacDonald; "The Frankfurt School
of Social Research and the Pathologization of Gentile Group Allegiances"