From 02fd0199ecfc42ac38fc971e799e7cc80e0071fc Mon Sep 17 00:00:00 2001 From: Linus Date: Tue, 29 Dec 2020 22:01:44 +0100 Subject: [PATCH] Added a docfile without any tools to generate the proper docs. I can't seem to find my old xslt files. --- documentation/doc.xml | 260 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 documentation/doc.xml diff --git a/documentation/doc.xml b/documentation/doc.xml new file mode 100644 index 0000000..21b7039 --- /dev/null +++ b/documentation/doc.xml @@ -0,0 +1,260 @@ + + + + Linus Björnstam + +
+

+ 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: +

    +
  • Portability. Any sufficiently masochistic programmer should be able to implement it in r7rs syntax-rules
  • +
  • A coherent, simple interface for looping over various kinds of data
  • +
  • A coherent, simple intefrace for accumulating data in loops, with support for accumulating data over sub-loops
  • +
  • A looping facility that in almost all cases produces as fast code as a hand-written named let
  • +
  • An extensible looping facility, where new ways of iterating over data can be easily added
  • +
+ + The only other lisp looping facility that I know of that provides these things is Common Lisps iterate package. Iterate does however do a lot of things that are cumbersome to do in portable scheme, and would be prohibitively complicated to implement efficiently in a portable way. +

+ + +

+ So, how does it look? A slightly contrived example, a naive sieve of Erathostenes: + + (define (erathostenes n) + (define vec (make-vector n #t)) + (loop/list ((:for i (up-from 2 (to n))) + :when (vector-ref vec i)) + (loop ((:for j (up-from (* 2 i) (to n) (by i)))) + (vector-set! vec j #f)) + i)) + + + Calling (erathostenes 10) returns a list of all primes below 10. +

+
+
+ +
+

+ The loop grammar is the following: + + (loop [name] (loop-clause ...) [=> final-expr] body ...) + + name = identifier + + loop-clause = (:for id id* ... seq-expr) + | (:acc id id* ... seq-expr) + | :when guard-expr + | :unless guard-expr + | :break break-expr + | :final guard-expr + | :subloop + + + If a name is provided, it will be bound to a macro that allows named update of loop variables: + + + (loop lp ((:for a (in 0 (+ a 1))) + :break (> a 9)) + => '() + (if (= 4 a) + (cons a (lp (=> a 8))) + (cons a (lp)))) + + + This rather inane example would return (0 1 2 3 4 8 9). +

+ + A subloop is a distinction between an outer loop and an inner loop. An for each element yielded by an outer loop, the inner loop is run until exhaustion. All non :for- or :acc-clauses break out a subloop. + + + (loop ((:for a (in-list '(1 2 3))) + :subloop + (:for b (up-from 0 (to a))) + (:acc acc (listing (cons a b)))) + => acc) + ;; => ((1 . 0) (2 . 0) (2 . 1) (3 . 0) (3 . 1) (3 . 2)) + + + The above :subloop clause is equivalent to :when #t and :unless #f. A :break clause will immediately stop execution of the loop: + + + (loop ((:for a (in-list '(1 2 3))) + :break (= 3 a) + (:for b (up-from 0 (to a))) + (:acc acc (listing (cons a b)))) + => acc) + ;; => ((1 . 0) (2 . 0) (2 . 1)) + + + And a :final guard will let one more body be evaluated. This clause is evaluated in the innermost loop, and as such only one body will be evaluated: + + + (loop ((:for a (in-list '(1 2 3))) + :final (= 3 a) + (:for b (up-from 0 (to a))) + (:acc acc (listing (cons a b)))) + => acc) + ;; =>((1 . 0) (2 . 0) (2 . 1) (3 . 0)) + + + + The pure 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. + + + (loop/list ((count (up-from 0 (to 100))) + (a (in 0 b)) + (b (in 1 (+ a b)))) + b) + + + +
+
(loop/first (clauses ...) body ...)
+
+

+ 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. +

+
+ +
(loop/last (clauses ...) body ...)
+
+

+ Returns the result of the last body to be evaluated. If no body is evaluated the return value is unspecified. +

+
+ +
(loop/list (clauses ...) body ...)
+
+

+ 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 +

+
+ +
(loop/product (clauses ...) body ...)
+
+

+ Multiplies each evaluated body. If no body is evaluated, 1 is returned +

+
+ +
(loop/sum (clauses ...) body ...)
+
+

+ Sums the result of each evaluated body. If no body is evaluated, 0 is returned. +

+
+ +
(loop/and (clauses ...) body ...)
+
+

+ 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. +

+
+ +
(loop/or (clauses ...) body ...)
+
+

+ 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.

+
+ +
(loop/list/parallel (clauses ...) body ...)
+
+

+ Like loop/list, but evaluates each body in parallel. +

+
+ +
+ +
+ + + +
+
(:for identifier (in start [update [stop]]))
+
+

+ Binds a loop variable to identifier. 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. + + + (loop ((:for a (in 0 b)) (:for b (in 1 (+ a b) (> b 20)))) + (display b) (newline)) + + Will print all fibonacci numbers below 20. +

+
+ +
(:for identifier (up-from start [(to bound)] [(by step)])
+
(:for identifier (up-from start [bound [by]]))
+
Binds identifier 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.
+ +
(:for identifier (down-from start [(to bound)] [(by step)])
+
(:for identifier (down-from start [bound [by]]))
+
Binds identifier to the number start 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.
+ +
(:for identifier [pair] (in-list expr [by])
+
Binds identifier 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.
+ +
(:for identifier [pairs] (in-lists expr [by])
+
Works the same as in-list, but expr must evaluate to a list of lists. identifier is bound to the car of those lists, and they are advanced by by, defaulting to cdr.
+ +
(:for identifier [index] (in-vector expr [low [high]]))
+
Binds identifier 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.
+ +
(:for identifier [index] (in-reverse-vector expr [high [low]]))
+
Binds identifier 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.
+ +
(:for identifier [index] (in-string expr [low [high]]))
+
Binds identifier 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.
+ +
(:for identifier [index] (in-reverse-string expr [high [low]]))
+
Binds identifier 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.
+ +
(:for identifier (in-port port [reader [eof?]]))
+
Binds identifier to the result of calling reader on port. Iteration stops when (eof? identifier) returns true.
+ +
(:for identifier (in-file path [reader [eof?]]))
+
Opens the file located at path (which is a string) and binds identifier to the result of calling reader on the opened port. Iteration stops when (eof? identifier) returns true.
+ +
(:for identifier (in-generator gen))
+
Binds identifier to the result of calling the SRFI-158-compatible generator gen. Iteration stops when gen returns the end-of-file object.
+ +
+ +
+ + + + + + + + +
+