We have all had the thought. Why oh why are not all our problems solved by a nice recursive algorithm over a list?. We see a neat problem. We smile and think about all the nice things we shall do right after finishing off this little tidbit, this delicious little bite of a problem. Then we scrape the surface. Under there is a jungle of state. Suddenly our little recursive list-mangling is not enough. To defeat the anxiety rising within us, we quickly write something using map, filter, zip and reduce. We test it. We smile again. We test it on real world data. The smile stiffens. The little bite has turned in to an o(n²) turd.
We raise our gaze and see the disgustingly mutating little wonders we could have achieved using python list comprehensions. We look sideways and see the magnificent, fantastic, but abominable loop macro in Common Lisp. Even they, the elitist ultra-productive scumbags, hate it, yet they sieze every moment to make underhanded comments about named lets. Why, oh why is the world not as elegant as we want it to be?
We think again about how python programmers write scheme, and shudder. "No! Never that!", we think to ourselves. A vague idea starts forming. A quote surfaces from the depths of our minds "Above all the wonders of Lisp's pantheon stand its metalinguistic tools; by their grace have Lisp's acolytes been liberated from the rigid ascetism of lesser faiths". Maybe we should try to do it ourselves. Thinking about it, we have actually written some macros before. Anaphoric ifs, some boilerplate removal macros, and oh! Even an almost-working implementation of SRFI-26. It passed nearly all the tests, and we couldn't be bothered to go any further. But a looping facility... How hard can it be?
-- Linus Björnstam, September 2020
Goof-loop is like the name suggests a looping facility. It is based in many ways on things I learned porting Racket's for-loops to guile and on the marvel that is (chibi loop) in chibi-scheme. The goals of goof-loop are:
So, how does it look? A slightly contrived example, a naive sieve of Erathostenes:
(erathostenes 10)
returns a list of all primes below 10.
The loop grammar is the following:
name
is provided, it will be bound to a macro that allows named update of loop variables:
(0 1 2 3 4 8 9)
.
:subloop
clause is equivalent to :when #t
and :unless #f
. A :break
clause will immediately stop execution of the loop:
loop
macro is quite a big hammer for most tasks. Often we want to do simple things, like collect elements into a list or a vector, which means the extra housekeeping of separating accumulators and for clauses are too much heavy lifting. goof-loop provides several simpler forms that can be used in those cases. In these simpler forms :acc is disallowed, and everything not identified as anything else is assumed to be a :for clause. The loop below accumulates the 100 first fibonacci numbers into a list.
If any body is ever evaluated, stop and return the value of that evaluation. If no body is ever evaluated the return value is unspecified.
Returns the result of the last body to be evaluated. If no body is evaluated the return value is unspecified.
Iterates over clauses
and builds a list of the result of every evaluation of body. The order of the list is the same as the order body was evaluated in. The result of the first evaluation of body is the first element of the resulting list.
The list returned is the same even when used with multi-shot continuations
If no body is evaluated, the result is the empty list
Multiplies each evaluated body. If no body is evaluated, 1 is returned
Sums the result of each evaluated body. If no body is evaluated, 0 is returned.
If all evaluated bodies return truthy, return the result of the last evaluated body. If any body returns #f, stop iteration and return #f. If no body is evaluated, #t is returned.
If any evaluated body returns truthy, stop iteration and return the result of that body. If no body returns truthy, return #f. If no body is evaluated, return #f.
Like loop/list, but evaluates each body in parallel.
Binds a loop variable to binding
. It's first value is start
. It is updated by the update
expression, or is left unchanged if no such expression is present. If a stop
expression is provided, it will be evaluated before each loop body. If the stop
expression returns true, the iteration will be considered exhausted.
binding
to the number start
up to bound
(exclusive!) by step
. If no bound
is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.binding
to the number (- start 1)
down to bound
(inclusive!) by step
. If no bound
is given, it will yield values indefinitely. The second shorter form will not allow unbounded iteratiom.binding
to the car of the loop variable pair
. pair
is advanced by applying the procedure by
to it (defaulting to cdr
). The iteration stops when pair
is the empty list.in-list
, but expr
must evaluate to a list of lists. binding
is bound to the car of those lists, and they are advanced by by
, defaulting to cdr
.binding
to all elements in the vector produced by expr
in order from low
to high
. low
defaults to 0 and high
defaults to the last index of the vector.binding
to all elements in the vector produced by expr
in reverse order from high
to low
. high
defaults to the last element of the vector and low
defaults to 0.binding
to all elements in the string produced by expr
in order from low
to high
. low
defaults to 0 and high
defaults to the last index of the string.binding
to all elements in the vector produced by expr
in reverse order from high
to low
. high
defaults to the last element of the vector and low
defaults to 0.binding
to the result of calling reader
on port
. Iteration stops when (eof? binding)
returns true.path
(which is a string) and binds binding
to the result of calling reader
on the opened port. Iteration stops when (eof? binding)
returns true.gen
. Iteration stops when gen
returns the end-of-file object.binding
to the (key . value)
pairs of the hash-table hash
. May, as all body-binding variables, be pattern-matched: