; Author: Juergen Lorenz ; ju (at) jugilo (dot) de ; ; Copyright (c) 2017-2019, Juergen Lorenz ; All rights reserved. ; ; Redistribution and use in source and binary forms, with or without ; modification, are permitted provided that the following conditions are ; met: ; ; Redistributions of source code must retain the above copyright ; notice, this list of conditions and the following dispasser. ; ; Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following dispasser in the ; documentation and/or other materials provided with the distribution. ; ; Neither the name of the author nor the names of its contributors may be ; used to endorse or promote products derived from this software without ; specific prior written permission. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #|[ This module implements some list creating routines, in particular collect, a version of Clojure's macro for, which is to replace list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0] of purely functional languages like Miranda. ]|# (module list-comprehensions (list-comprehensions collect range repeat iterate-while iterate-until iterate-times) (import scheme (only (chicken base) case-lambda error print)) (define range (case-lambda ((upto) (if (>= upto 0) (range 0 upto 1) (range 0 upto -1))) ((from upto) (if (>= upto from) (range from upto 1) (range from upto -1))) ((from upto step) (cond ((and (>= upto from) (positive? step)) (let loop ((k from) (result '())) (if (>= k upto) (reverse result) (loop (+ k step) (cons k result))))) ((and (< upto from) (negative? step)) (let loop ((k from) (result '())) (if (<= k upto) (reverse result) (loop (+ k step) (cons k result))))) (else (error 'range "wrong sign of" step)))))) (define repeat (case-lambda ((times) (lambda (x) (repeat times x))) ((times x) (let loop ((k 0) (result '())) (if (= k times) result (loop (+ k 1) (cons x result))))) )) (define iterate-while (case-lambda ((fn ok?) (lambda (start) (iterate-while fn ok? start))) ((fn ok? start) (let loop ((var start) (result '())) (if (ok? var) (loop (fn var) (cons var result)) (reverse result)))) )) (define iterate-until (case-lambda ((fn ok?) (lambda (start) (iterate-until fn ok? start))) ((fn ok? start) (let loop ((var start) (result '())) (if (ok? var) (reverse result) (loop (fn var) (cons var result))))) )) (define iterate-times (case-lambda ((fn k) (lambda (start) (iterate-times fn k start))) ((fn k start) (let loop ((var start) (n 0) (result '())) (if (= n k) (reverse result) (loop (fn var) (+ n 1) (cons var result))))) )) ;;; (collect item (var lst fltr ...) ....) ;;; ---------------------------------- (define-syntax collect (syntax-rules () ((_ item (var lst fltr ...)) (let recur ((seq lst)) (if (null? seq) '() (let ((var (car seq))) (if (and fltr ...) (cons item (recur (cdr seq))) (recur (cdr seq))))))) ((_ item (var lst fltr ...) (var1 lst1 fltr1 ...) ...) (let recur ((seq lst)) (if (null? seq) '() (let ((var (car seq))) (if (and fltr ...) (append (collect item (var1 lst1 fltr1 ...) ...) (recur (cdr seq))) (recur (cdr seq))))))) )) ;;; (list-comprehensions sym ..) ;;; ---------------------------- ;;; documentation procedure (define list-comprehensions (let ((als '( (list-comprehensions procedure: (list-comprehensions sym ..) "documentation procedure") (range procedure: (range upto) (range from upto) (range from upto step) "creates a list of numbers with given limits" "from defaults to 0" "step defaults to 1") (repeat procedure: (repeat times) (repeat times x) "returns a list with times items x") (iterate-times procedure: (iterate-times fn k) (iterate-times fn k start) "returns a list by applying fn successively to start" "k times") (iterate-while procedure: (iterate-while fn ok?) (iterate-while fn ok? start) "returns a list by applying fn successively to start" "as long as ok? succeeds") (iterate-until procedure: (iterate-until fn ok?) (iterate-until fn ok? start) "returns a list by applying fn successively to start" "until ok? succeeds") (collect macro: (collect item (var lst fltr ...) ....) "creates a new list by binding var to each element" "of the list lst in sequence, and if it passes the checks," "fltr ..., inserts item into the result list." "The qualifieres, (var lst fltr ...), are processed" "sequentially from left to right, so that filters of a" "qualifier have access to the variables of qualifiers" "to its left.") ))) (case-lambda (() (map car als)) ((sym) (let ((pair (assq sym als))) (if pair (for-each print (cdr pair)) (error "Not in list" sym (map car als)))))))) ) ; module list-comprehensions