| 1 |
;; Copyright (C) Marc Nieper-Wißkirchen (2023). All Rights Reserved. |
| 2 |
;; |
| 3 |
;; SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
;; Permission is hereby granted, free of charge, to any person |
| 6 |
;; obtaining a copy of this software and associated documentation |
| 7 |
;; files (the "Software"), to deal in the Software without |
| 8 |
;; restriction, including without limitation the rights to use, copy, |
| 9 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 |
;; of the Software, and to permit persons to whom the Software is |
| 11 |
;; furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
;; The above copyright notice and this permission notice shall be |
| 14 |
;; included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
;; SOFTWARE. |
| 24 |
|
| 25 |
(import |
| 26 |
(rename (rnrs) |
| 27 |
(partition rnrs:partition)) |
| 28 |
(srfi :247 syntactic-monads)) |
| 29 |
|
| 30 |
(assert (equal? '(3 2 4) |
| 31 |
(let () |
| 32 |
(define-syntactic-monad $ a b) |
| 33 |
(letrec ([f ($ lambda (c) |
| 34 |
($ list ([a (+ a b)]) c))]) |
| 35 |
(f 1 2 4))))) |
| 36 |
|
| 37 |
(assert (equal? '(3 2 4) |
| 38 |
(let () |
| 39 |
(define-syntactic-monad $ a b) |
| 40 |
(letrec ([f ($ case-lambda |
| 41 |
[(c) |
| 42 |
($ list ([a (+ a b)]) c)])]) |
| 43 |
(f 1 2 4))))) |
| 44 |
|
| 45 |
(assert (equal? '(3 2 4) |
| 46 |
(let () |
| 47 |
(define-syntactic-monad $ a b) |
| 48 |
($ define (f c) |
| 49 |
($ list ([a (+ a b)]) c)) |
| 50 |
(f 1 2 4)))) |
| 51 |
|
| 52 |
(assert (equal? '(3 2 4) |
| 53 |
(let () |
| 54 |
(define-syntactic-monad $ a b) |
| 55 |
($ let f ([a 1] [b 2] [c 4]) |
| 56 |
($ list ([a (+ a b)]) c))))) |
| 57 |
|
| 58 |
(define partition |
| 59 |
(lambda (pred ls) |
| 60 |
(define-syntactic-monad $ in out) |
| 61 |
(let f ([ls ls]) |
| 62 |
(if (null? ls) |
| 63 |
($ values ([in '()] [out '()])) |
| 64 |
($ let*-values ([() (f (cdr ls))]) |
| 65 |
(let ([x (car ls)]) |
| 66 |
(if (pred x) |
| 67 |
($ values ([in (cons x in)])) |
| 68 |
($ values ([out (cons x out)]))))))))) |
| 69 |
|
| 70 |
(assert (equal? '((one four five) (2 3 6)) |
| 71 |
(let-values ([(in out) |
| 72 |
(partition symbol? '(one 2 3 four five 6))]) |
| 73 |
(list in out)))) |
| 74 |
|
| 75 |
(define factor |
| 76 |
(lambda (n) |
| 77 |
(define-syntactic-monad $ n i) |
| 78 |
($ let f ([i 2]) |
| 79 |
(cond |
| 80 |
[(>= i n) (list n)] |
| 81 |
[(integer? (/ n i)) |
| 82 |
(cons i ($ f [(n (/ n i))]))] |
| 83 |
[else ($ f [(i (+ i 1))])])))) |
| 84 |
|
| 85 |
(assert (equal? '(2 2 2 2 2 2 2 2 3 3 3 3 5 5 7) |
| 86 |
(factor 3628800))) |
| 87 |
|
| 88 |
(define run1 |
| 89 |
(lambda (prog a b c) |
| 90 |
(define a->b |
| 91 |
(lambda (prog a b c) |
| 92 |
(run1 prog (cdr a) (cons (car a) b) c))) |
| 93 |
(define a->c |
| 94 |
(lambda (prog a b c) |
| 95 |
(run1 prog (cdr a) b (cons (car a) c)))) |
| 96 |
(define b->a |
| 97 |
(lambda (prog a b c) |
| 98 |
(run1 prog (cons (car b) a) (cdr b) c))) |
| 99 |
(define b->c |
| 100 |
(lambda (prog a b c) |
| 101 |
(run1 prog a (cdr b) (cons (car b) c)))) |
| 102 |
(define c->a |
| 103 |
(lambda (prog a b c) |
| 104 |
(run1 prog (cons (car c) a) b (cdr c)))) |
| 105 |
(define c->b |
| 106 |
(lambda (prog a b c) |
| 107 |
(run1 prog a (cons (car c) b) (cdr c)))) |
| 108 |
(if (null? prog) |
| 109 |
(values a b c) |
| 110 |
(let ([ins (car prog)] [prog (cdr prog)]) |
| 111 |
(case ins |
| 112 |
[(a->b) (a->b prog a b c)] |
| 113 |
[(a->c) (a->c prog a b c)] |
| 114 |
[(b->a) (b->a prog a b c)] |
| 115 |
[(b->c) (b->c prog a b c)] |
| 116 |
[(c->a) (c->a prog a b c)] |
| 117 |
[(c->b) (c->b prog a b c)]))))) |
| 118 |
|
| 119 |
(assert (equal? '(() () (1 2)) |
| 120 |
(let-values ([(a b c) |
| 121 |
(run1 '(a->b b->c a->c) '(2 1) '() '())]) |
| 122 |
(list a b c)))) |
| 123 |
|
| 124 |
(define-syntactic-monad $ prog a b c) |
| 125 |
(define run2 |
| 126 |
($ lambda () |
| 127 |
($ define (a->b) |
| 128 |
($ run2 ([a (cdr a)] [b (cons (car a) b)]))) |
| 129 |
($ define (a->c) |
| 130 |
($ run2 ([a (cdr a)] [c (cons (car a) c)]))) |
| 131 |
($ define (b->a) |
| 132 |
($ run2 ([a (cons (car b) a)] [b (cdr b)]))) |
| 133 |
($ define (b->c) |
| 134 |
($ run2 ([b (cdr b)] [c (cons (car b) c)]))) |
| 135 |
($ define (c->a) |
| 136 |
($ run2 ([a (cons (car c) a)] [c (cdr c)]))) |
| 137 |
($ define (c->b) |
| 138 |
($ run2 ([b (cons (car c) b)] [c (cdr c)]))) |
| 139 |
(if (null? prog) |
| 140 |
(values a b c) |
| 141 |
(let ([ins (car prog)] [prog (cdr prog)]) |
| 142 |
(case ins |
| 143 |
[(a->b) ($ a->b)] |
| 144 |
[(a->c) ($ a->c)] |
| 145 |
[(b->a) ($ b->a)] |
| 146 |
[(b->c) ($ b->c)] |
| 147 |
[(c->a) ($ c->a)] |
| 148 |
[(c->b) ($ c->b)]))))) |
| 149 |
|
| 150 |
(assert (equal? '(() () (1 2)) |
| 151 |
(let-values ([(a b c) |
| 152 |
(run2 '(a->b b->c a->c) '(2 1) '() '())]) |
| 153 |
(list a b c)))) |
| 154 |
|
| 155 |
;; Local Variables: |
| 156 |
;; mode: scheme |
| 157 |
;; End: |