Re: What is the main advantage of macros over functions?



In article <1e7dnnx8u08ko.11fu2qz89g3g2$.dlg@xxxxxxxxxx>,
Frank Buss <fb@xxxxxxxxxxxxx> wrote:

> Rainer Joswig wrote:
>
> > Actually, if one is allergic to reading Lambda one may better not use
> > Lisp.
> >
> > Not liking to type or read LAMBDA is at the same level of not liking
> > parentheses.
>
> I don't care about parentheses, because you don't need to read them very
> often, if the code is properly indented. But with LAMBDA you have to read
> and think about it, when it is used. Perhaps it should be written λ, then
> it would be easier to read.

Of course you have to think about it. If LAMBDA does not make
you think, then it would not be useful. Avoiding LAMBDA because
of 'low readability' is just another 'beginner' error.

Using the greek lambda character, which I have seen used by some
Lisp authors, doesn't help you much, I'd say.

>
> > The bit longer reading pays back big if you use the advantage of
> > a functional programming style.
> > Macros can't be passed around. Functions can be.
>
> Yes, this is useful sometimes, but for me the with-* idiom is one of the
> cases where a macro is simpler, something the Common Lisp designers have
> thought, too. I can't imaging a real world example, where you want to pass
> with-open-file like a function to another function.

No?

(defun curry (function arg)
(lambda (&rest args)
(apply function arg args)))

(defun call-with-stream (function file)
(with-open-file (stream file)
(funcall function stream)))

(defun count-lines-in-stream (stream)
(loop for line = (read-line stream nil nil)
while line count line))

Now you want to count lines of some files.


CL-USER 5 > (reduce #'+
(mapcar
(curry #'call-with-stream #'count-lines-in-stream)
(directory "http:lw;**;.lisp")))
5559

Not many LAMBDAs here, but combining functions.

It is called Functional Programming and pretty powerful.

>
> > Readmacros are a completely different mechanism. Macros and Readmacros
> > are different things.
>
> They are both macros, otherwise the name "reader macro" wouldn't make
> sense.

With this reasoning ships and spaceships are both ships. Well, yes.
And both have a captain.

> Both generates code,

The purpose of READMACROs is not to generate code.
The purpose is to transform characters from input streams to Lisp
data. It is perfectly legal and useful to generate data
with READMACROs.

Say you have a file that contains lines with some encoding of data.
It is perfectly fine to use a readmacro in the text and to read the data
using READ. See also the XAPPING example in CLtL2.

Readmacros may also be used for just consuming some characters
from a stream. For example If you have a readmacro for comments,
the comment might just be ignored, but read from the stream.

> the difference is that macros defined with
> defmacro are expanded at compile time and reader macros at read time and
> how you pass parameters to the macro generating function.

There are all kinds of differences.

Readmacros
- input is a stream
- output is Lisp data or even no data
- Readmacros are used to extend the READer
- the API is completely different from Macros

Macros
- input is a Lisp form
- output is some Lisp data
- Macros are used from the macro expander
(which is used by EVAL, COMPILE, COMPILE-FILE, ...)

Again, readmacros and macros are very different mechanisms.
Readmacros are not a sub-concept of macros.

> But I agree that you should use macros only for really useful cases, like
> for the infix reader macro or implementing patterns like the with-* pattern
> (a similar pattern is know as RAII in C++), which normally are not passed
> as function arguments.
>
> BTW: I'm not allergic to λ, but perhaps I like macros more than you :-)
> http://www.frank-buss.de/lisp/functional.html

Yes, that's Functional Geometry. Lisp already supports
Functional Programming. Functional Geometry is
an application of Functional Programming.
If you find Functional Geometry useful, you may find
Functional Programming in general useful.
Just as you combine functions to generate more
complex pictures, you combine functions in Lisp
for 'ordinary' programming. Say, you can write
Functional File Handling.

>
> > See also the 'Lambda Nature Fortune Cookies':
> >
> > http://www.gotlisp.com/lambda/lambda.txt
>
> Nice. I like this one:
>
> === ALL USERS PLEASE NOTE ========================
>
> There has been some confusion concerning MAPCAR.
> (DEFUN MAPCAR (&FUNCTIONAL FCN &EVAL &REST LISTS)
> (PROG (V P LP)
> (SETQ P (LOCF V))
> L (SETQ LP LISTS)
> (%START-FUNCTION-CALL FCN T (LENGTH LISTS) NIL)
> L1 (OR LP (GO L2))
> (AND (NULL (CAR LP)) (RETURN V))
> (%PUSH (CAAR LP))
> (RPLACA LP (CDAR LP))
> (SETQ LP (CDR LP))
> (GO L1)
> L2 (%FINISH-FUNCTION-CALL FCN T (LENGTH LISTS) NIL)
> (SETQ LP (%POP))
> (RPLACD P (SETQ P (NCONS LP)))
> (GO L)))
> We hope this clears up the many questions we've had about it.

--
http://lispm.dyndns.org/
.



Relevant Pages

  • Re: New book about Common Lisp: Let Over Lambda
    ... I have read every lisp book I've been able to get my hands on ... Lisp macros are not given nearly enough ... I personally consider Let Over Lambda to be an unofficial sequel ... integral parts of the language. ...
    (comp.lang.lisp)
  • Re: What is the main advantage of macros over functions?
    ... In the Lambda Order they are all first-class. ... >> Only advantage of Lisp macros over Lisp functions I see are ... > For me the main advantage with macros is to write less code, ... Readmacros are a completely different mechanism. ...
    (comp.lang.lisp)
  • Re: Macros still needed with shorter LAMBDA?
    ... > I've been thinking about the use of macros in Lisp, ... > want to keep typing LAMBDA. ... Macros abstract over syntax. ... To get a feel for the issue, try writing CASE or COND as an ordinary ...
    (comp.lang.lisp)
  • New book about Common Lisp: Let Over Lambda
    ... I have read every lisp book I've been able to get my hands on ... Lisp macros are not given nearly enough ... I personally consider Let Over Lambda to be an unofficial sequel ... integral parts of the language. ...
    (comp.lang.lisp)
  • Re: quote semantics
    ... with some useful work on Lisp libraries, if you fall that way GTF ... ask for that one, too, whether it was all that related to the Lambda ... And certainly CL took influence from Scheme, ... claim that this community, which is entirely composed of people who, ...
    (comp.lang.lisp)