4 ms·
>Most lisp docs will tell you to use macros only when necessary One also declares variables when necessary, and one also creates arrays when necessary, etc. Bu
by flavio81 9y ago
>Most lisp docs will tell you to use macros only when necessary
One also declares variables when necessary, and one also creates arrays when necessary, etc. But imagine a programming language that doesn't support arrays. It would be a nightmare if you need to do certain scientific computations.
So in the same way, yes, not having a (proper, Lisp-like) macro system surely hurts a lot, once you realize how it makes certain problems become really easy.
And, by the way, one should write macros when necessary. In Lisp, we're using macros most of the time!
>Isn't loop essentially a macro (probably special op) that a lot of people hate?
And other Lisp programmers like the LOOP macro, since it can allow for very readable and concise code to do something simple that should stay simple to read.
Examples:
(loop for x from 1
for y = (* x 10)
while (< y 100)
do (print (* x 5))
collect y)
Result:
5
10
15
20
25
30
35
40
45
(10 20 30 40 50 60 70 80 90)
Example 2:
(loop for x in '(a b c d e)
for y in '(1 2 3 4 5)
collect (list x y) )
Result:
((A 1) (B 2) (C 3) (D 4) (E 5))
Source of examples: http://cl-cookbook.sourceforge.net/loop.html http://cl-cookbook.sourceforge.net/loop.html
- gowld 9y agothat loop code looks hideous. For the first: There's an implicit x->x+1 hiding in there? Why 'for y' if y is derived from x and not incremented on its own? For the second: It looks like a nested (cross product) loop, but it's actually a zip (dot product) loop?
- kazinator 9y agoIt certainly does not look like it is one way or the other; it is something you have to know. If you don't know anything about loop, but have a belief that it is doing a Cartesian product, or a belief that it is not doing one, your belief has no rational basis either way. You can infer which one is right from the output. loop doesn't do cross-producting; if you know that, there is no mistaking it. All the clauses specify iteration elements for one single loop. "for x from 1" means starting at 1, in increments of 1. "for y = expr" means that expr is evaluated on each iteration, and y takes on that value. y could be incremented on its own, but then that example wouldn't show the "for var = expr" syntax, how one variable can depend on a combination of others.
- kazinator 9y agoThere are some legitimate reasons to dislike loop. It's a high level construct, yet it has unspecified behaviors. A program can be nonportable on account of some manner of using loop. It's been the case in the past that Lisp applications ended up carrying their own private loop implementation which would behave the same way everywhere. Today there is a related discussion in comp.lang.lisp: https://groups.google.com/d/msg/comp.lang.lisp/U5LqiM5nKq8/Mz0PzmL5AQAJ https://groups.google.com/d/msg/comp.lang.lisp/U5LqiM5nKq8/M...