P
Peter Seibel
Joachim Durchholz said:Um, right, but that's just a question of having the right syntactic sugar.
Uh right, that's what macros are for, providing the syntactic sugar.
-Peter
Joachim Durchholz said:Um, right, but that's just a question of having the right syntactic sugar.
Peter said:sugar.
Uh right, that's what macros are for, providing the syntactic sugar.
Joachim Durchholz said:This statement is wrong if left in full generality: higher-order
functions can control quite precisely what gets evaluated when.
Anton van Straaten said:Yes, but the point is that with a concise syntax for lambda, entire
classes of macros can become unnecessary. That's how Smalltalk
handles 'if', for example - no macros or special forms needed.
[...]Okay, so I picked an unfortunate example in that it also falls in the
class of macros that become unecessary when other bits of syntactic
sugar are provided. How about this one.
Peter said:Okay, so I picked an unfortunate example in that it also falls in the
class of macros that become unecessary when other bits of syntactic
sugar are provided.
Marcin said:I'm not saying that the cost is obviously too high but that it's a
tradeoff, macros don't come for free. And that there are other ideas which
can replace many macros with different tradeoffs: namely a richer builtin
syntax and concise anonymous functions. In a lazy language there are yet
fewer uses of macros.
Marcin 'Qrczak' Kowalczyk said:The remaining uses of macros are so rare ...
[1] Even if the uses of macros *were* rare that's doesn't seem to be
the correct metric to balance against the "costs" of macros. Normally
we weigh costs versus benefits, right? So in a way, the higher
non-Lispers estimate the "costs" of macros (need for an code-as-data
syntax, interpreter built into the compiler, etc.), the higher they
should estimate the benefits. At least if they give Lisp programmers
*any* credit at all for making a rational choice in tool selection.
Because Common Lispers love macros and find the benefits *far*
outweigh the costs. But I guess you can explain that by assuming we're
deluded.
Pascal Costanza said:The essence of this is as follows:
(defmacro my-cond (&body body)
`(cond ,@(append body '(t (handle-the-default-case))))
CL-USER 91 > (econd
((= 3 4) "foo")
((= 4 4) "bar"))
"bar"
CL-USER 92 > (econd
((= 3 4) "foo")
((= 4 5) "bar"))
Error: fell through ECOND form.
Joachim Durchholz said:If that's the case, it's probably more due to lack of demand than due
to serious technical issues.
and [quoting will lead to] less intuitive code.
Efficiency issues aside: how are macros more intuitive than quoting?
Coby Beck said:Just to provide a more apparently general (and working version, analogous
to CL's ECASE:
CL-USER 90 >
(defmacro econd (&body body)
`(cond ,@(append body
`((t (error (format nil
"fell through ECOND form. could not
satisfy any of the following: ~{~%~A~}~%"
(mapcar #'(lambda (cond)
(car cond))
',body))))))))
Dirk said:Since the overhead of evaluating it at runtime is minimal, especially
with lazyness, that's exactly the situation where it is natural to use
a HOF instead of a macro.
^^^^^^^^I didn't go through the two proposed Lisp solutions in detail, but here's
a HOF in Haskell that does the same. First a general 'cond' with an
explicit default case, then 'econd' based on cond with an error as
default case:
cond :: a -> [(Bool, a)] -> a
cond def [] = def
cond def ((True,x):_) = x
cond def ((False,_):xs) = econd xs
econd :: [(Bool, a)] -> a
econd = cond (error "Default econd case")
CL-USER 91 > (econd
((= 3 4) "foo")
((= 4 4) "bar"))
"bar"
CL-USER 92 > (econd
((= 3 4) "foo")
((= 4 5) "bar"))
Error: fell through ECOND form. could not satisfy any of the following:
(= 3 4)
(= 4 5)
Lex said:They don't let you execute stuff at compile time.
Joachim Durchholz said:And, of course, macros can evaluate at compile time. Personally, I'd
prefer to do compile-time evaluation based on "the compiler will
evaluate all known-to-be-constant expressions". The advantage here
is that programmers don't need to learn another sublanguage for
compile-time expressions.
Peter said:Ah, but in Lisp we don't have to. We use Lisp.
Joachim said:Lisp-the-language is a quite pretty lean-and-mean KISS language. The
stuff that's built on top of it (macros, readers, dispatch mechanisms,
etc. etc.) is neither lean nor KISS
Joachim Durchholz said:Having readers and special forms /is/ an extra sublanguage. I don't
have to learn extra syntax for these forms (which is good), but I do
have to learn about a lot of special rules that apply to macros and
nothing else (which is not so good).
Letting the compiler evaluate what it can means that I don't even
have to learn extra forms.
Actually, that's one of the reasons that keeps my from trying out a
modern Lisp: I'd have to learn all these extra forms, and I've got a
feeling that macrology à la Lisp is oversophisticated for something
as simple as compile-time evaluation.
I'm pretty sure that macros solve more problems than just
compile-time evaluation.
Yup.
I just suspect that better solutions are available in every case,
and I not just suspect but know that macros have some very serious
disadvantages (such as bad debugger interaction, a potential for
really ugly hairballs, and a constant temptation for stopgap
solutions that "work well enough").
Lisp-the-language is a quite pretty lean-and-mean KISS language. The
stuff that's built on top of it (macros, readers, dispatch
mechanisms, etc. etc.) is neither lean nor KISS nor (IMHO) pretty -
YMMV.
Or, more to the point: I have yet to see something that cannot be
done in a leaner, more KISS way.
Which is why I'm going to stick with functional languages. After
all, the higher-order stuff was what attracted me to Lisp in the
first place, the rest of the language less than impressed me. I'll
grant that modern Lisps have found ways around any problems
(otherwise, modern Lisps wouldn't be in use), but why mess with
workarounds if I can have the cake (higher-order programming) and
eat it, too (amenability to static analysis)?
Peter Seibel said:them. Now I don't know Haskell or ML so I'm also suffering from finite
knowledge. Maybe one day I'll have time to learn one of them for
myself and see if they really do offer better solutions.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.