4 ms·
Yes, I was a fan of LOOP too, until I found iterate. I mean, we are discussing the two things comparatively here, aren't we? So, how is loop better than iterat
by maydup-nem 5y ago
Yes, I was a fan of LOOP too, until I found iterate.
I mean, we are discussing the two things comparatively here, aren't we? So, how is loop better than iterate? Iterate has nearly the same semanitcs as LOOP. It's basically LOOP on s-exprs, with some handy idioms sprinkled in. But often times the match is 1-1. So, how is the absence of sexprs making anything easiers to read or write? It makes it harder for me in both cases.
- taeric 5y agoI'll try iterate. For me it is just that loop is already very easy to read. Moving all things to s exprs doesn't seem like it would be that big of a deal. And I should say that the construct that I find easiest is the "then" clause. (Loop repeat 8 For a = 0 then b For b = 1 then c For c = 1 then (+ a b) Collect a) That just reads very easily to me. (Typed on phone...)
- maydup-nem 5y agoFirst of all, if you are going to try it out, I recommend using this macro: (defmacro itr (&rest rest) `(iterate:iterate ,@rest)) because it keeps all the clauses on the same indentation level. Now, to the translation: iterate has previous clause. https://iterate.common-lisp.dev/doc/Previous-Values-of-Driver-Variables.html https://iterate.common-lisp.dev/doc/Previous-Values-of-Drive... which may help translate the loop form you provided into this: (itr (repeat 8) (for a previous b initially 0) (for b previous c initially 1) (for c = (+ a b)) (collect a)) However, if you wanted that exact syntax, you could try: (defmacro-clause (FOR var = init-value THEN form) `(progn (with ,var = ,init-value) (after-each (setf ,var ,form)))) to get a direct translation: (itr (repeat 8) (for a = 0 then b) (for b = 1 then c) (for c = 1 then (+ a b)) (collect a)) > Moving all things to s exprs doesn't seem like it would be that big of a deal. One of the biggest things you will "miss" from LOOP is the do keyword. The control structures is just regular lisp code. Also, I found generators to be pretty handy (not often, but I am glad I had them when it was called for). I mean, it's not exactly groundbreaking, but it does make a difference especially that a looping construct is probably the heaviest-used macro. You can certainly get away with LOOP, but I see iterate as more or less how LOOP should've been done. I also recommend reading this short discussion in the manual https://iterate.common-lisp.dev/doc/Don_0027t-Loop-Iterate.html#Don_0027t-Loop-Iterate https://iterate.common-lisp.dev/doc/Don_0027t-Loop-Iterate.h...
- taeric 5y agoPrevious doesn't have the same flow, so does feel more cumbersome than I'd like. (That is, I specifically like the left to right flow of "this then that" for variables.) I do have a softspot for how this is embracing the parens. :D This is funny in that it seems to dodge the biggest complaint of loop, that it has different syntax for lists and vectors/hashtables.
- maydup-nem 5y agoIt has in-sequence, however it uses elt I think. For efficiency reasons, there's a separation. Otherwise how should it work? In any case, you could probably write a macro that does the right thing based on type. > Previous doesn't have the same flow, I have provided a clause macro that uses the exact syntax you showed, you don't have to use previous.
- taeric 5y agoApologies, I was agreeing with your macro on why I like "then" over "previous". I did not intend mine as a correction or challenge. My point on "in-sequence" and such was more that the main complaint I ever hear regarding LOOP is that: (loop for c across "hello" collect c) is not the same as (loop for c in '(#\h #\e #\l #\l #\o) collect c) Specifically, "in" versus "across" seems to upset a lot of folks. I don't recall ever hearing too many complaints about the syntax not being more nested. Again, though, looks neat and I'm excited to dive more into it.
- maydup-nem 5y agoOkay, gotcha. Yeah, I can see how having distinct keywords may be seen as inconvenient. I think if there were a general container-keyword that wouldn't introduce any cost based on a type declaration, that would be cool, but it's not something I have encountered a real need for.
- reikonomusha 5y agoLOOP is better than ITERATE because I don't have to load a library to use it, and I don't have to tell my colleagues learning Lisp to "please ignore the built in iteration construct and instead use the one that has more parentheses from this library." Also note that LOOP is an S-expression. Just because there are more sub-lists doesn't make it more or less of an S-expression.
- maydup-nem 5y ago> Also note that LOOP is an S-expression. Well, it's body doesn't look like s-expressions to me, that's the point. And there's 0 reasons why they shouldn't be. But, conversely, there are real benefits to them being sexps. Coherency, for one. It's not the first time I hear this defense: "it's part of CL standard therefore it's lispy", "it's just a DSL". But these are qualities, aren't they? Some things will look lispier that other things. Some DSLs are better designed. Look at the DO keyword. Why exactly do I need it? Why do I need :when or :if when I have cl:when and cl:if? > don't have to load a library to use it, and I don't have to tell my colleagues That's a valid point, this is the price you would have to pay (and don't forget importing the symbols!). But you just might have to gain more in the long term, if you switch. I for one, don't mind, but It Depends TM. If I had a large project nearing its completion that uses LOOP, i wouldn't switch to iterate, sure. But on a new project? I might as well convince everybody else just to try it out for the hell of it.