I don't understand why turning a simple loop into a 'mindbend' is considered good. The downfall of programming is complexity, if you're getting your mind blown by a loop how are you going to do the rest of the program?
mindbending can also refer to something being deceptively simple. you might think it would be a big complicated mess, but using this one weird trick makes it really obvious what's going on.
How does that relate to a simple loop construct though? Why would you want that to be mind bending in interface or implementation? Every other language makes it as simple as possible.
This isn't really true – you have languages like Odin that only have a for loop, no while loop, that only supports index-based iteration. Then you have languages like Python that let you loop over an arbitrary iterable, and define your own iterables. Some languages allow conditionals in loops, some don't. Some let you loop over multiple iterables, while some only take one at a time.
Common Lisp happens to be on the upper end of what loop allows – you can use it as a standard for loop pretty easily, but the interface gives you many other options.
> Common Lisp happens to be on the upper end of what loop allows – you can use it as a standard for loop pretty easily, but the interface gives you many other options.
If you really wanna get freaky try 'do. It is the heroin addicted cousin of 'loop
yes the syntax for 'do is simple, like that of lisp. however 'do allows you to make far more complex iteration constructs than 'loop. 'loop is just a DSL to make some of these constructs more concise. read up on it
CL-USER 18 > (do ((a 1 (+ a 1))
(b 10 (* b 1.5))
(c nil))
((> a 5) (list a b (reverse c)))
(push (* a b) c))
(6 75.9375 (10 30.0 67.5 135.0 253.125))
CL-USER 19 > (loop for a = 1 then (+ a 1)
and b = 10 then (* b 1.5)
and c = NIL then c
when (> a 5) do (return (list a b (reverse c)))
do (push (* a b) c))
(6 75.9375 (10 30.0 67.5 135.0 253.125))
You can also express LOOP constructs in terms of DO. However if you were to construct a more exotic iterator that is not so straight forward in LOOP (beware of edge cases), I think it is more reasonable to pick DO. I think also that your example illustrates this.
Of course to each their own. I like LOOP a lot actually when I need to do something familiar, however for something unfamiliar DO is often my choice. It also serves as a caution to tread and think carefully when I return to the code. Sometimes, after a while, I realise how to do the DO construct succintly with LOOP
And then there's Scheme, where there are no iterative loops; all looping is done with recursion. You can build pretty much everything other languages do with loops on top of that, though.
The mindbend was more of my approach to the construct. It began with disdain before even really using it much. Looking back, I really couldn't articulate what I disliked about it.
For an even sillier mind bend, I'm using tagbody to be able to directly transcribe some of Knuth's algorithms as I am learning them.