3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
23 ;;; srfi-1.mes is the minimal srfi-1 needed to run mescc.
27 (define (find pred lst)
30 (if (pred (car lst)) (car lst)
33 (define (filter pred lst)
37 (cons (car lst) (loop (cdr lst)))
40 (define (append-map f lst . rest)
41 (apply append (apply map f (cons lst rest))))
43 (define (filter-map f h . t)
46 (let ((r (f (car h))))
47 (if r (cons r (filter-map f (cdr h)))
48 (filter-map f (cdr h))))
50 (let ((r (f (car h) (caar t))))
51 (if r (cons r (filter-map f (cdr h) (cdar t)))
52 (filter-map f (cdr h) (cdar t))))
53 (error 'unsupported (cons* "filter-map 3:" f h t))))))
55 (define (fold proc init lst1 . rest)
57 (let loop ((lst1 lst1) (result init))
58 (if (null? lst1) result
59 (loop (cdr lst1) (proc (car lst1) result))))
60 (if (null? (cdr rest))
61 (let loop ((lst1 lst1) (lst2 (car rest)) (result init))
64 (loop (cdr lst1) (cdr lst2) (proc (car lst1) (car lst2) result))))
65 (let loop ((lst1 lst1) (lst2 (car rest)) (lst3 (cadr rest)) (result init))
69 (loop (cdr lst1) (cdr lst2) (cdr lst3) (proc (car lst1) (car lst2) (car lst3) result))))
70 (error "FOLD-4-NOT-SUPPORTED"))))
72 (define (fold-right proc init lst1 . rest)
74 (let loop ((lst lst1))
76 (proc (car lst) (loop (cdr lst)))))
77 (error "FOLD-RIGHT-2-NOT-SUPPORTED")))
79 (define (unfold p f g seed . rest)
80 (let ((tail-gen (if (null? rest) (const '())
82 (define (reverse+tail lst seed)
84 (result (tail-gen seed)))
85 (if (null? lst) result
87 (cons (car lst) result)))))
88 (let loop ((seed seed) (result '()))
89 (if (p seed) (reverse+tail result seed)
91 (cons (f seed) result))))))
93 (define (remove pred lst) (filter (lambda (x) (not (pred x))) lst))
95 (define (reverse! lst . term)
96 (if (null? term) (core:reverse! lst term)
97 (core:reverse! lst (car term))))
99 (define (srfi-1:member x lst eq)
101 (if (eq x (car lst)) lst
102 (srfi-1:member x (cdr lst) eq))))
104 (define mes:member member)
106 (define (member x lst . rest)
107 (if (null? rest) (mes:member x lst)
108 (srfi-1:member x lst (car rest))))
110 (define mes:iota iota)
112 (define (srfi-1:iota n start step)
114 (cons start (srfi-1:iota (- n 1) (+ start step) step))))
116 (define (iota n . rest)
117 (if (null? rest) (mes:iota n)
118 (let ((start (car rest))
119 (step (if (null? (cdr rest)) 1
121 (srfi-1:iota n start step))))
123 (define last (compose car last-pair))
125 (define (delete-duplicates lst . equal)
126 (let ((equal (and (pair? equal) (car equal))))
127 (let loop ((lst lst))
129 (if (if equal (member (car lst) (cdr lst) equal)
130 (member (car lst) (cdr lst)))
132 (cons (car lst) (loop (cdr lst))))))))
134 (include-from-path "srfi/srfi-1.scm")