chickadee » simple-binds

Rationale

A simplified implemention of pattern matching and bindings. Only nested sequeces of pseudo-lists are considered. Most macros are based on bind, a version of Common Lisp's destructuring-bind. But bind-case is enhanced with a where clause, which makes this macro much more expressive.

API

bind


(bind pat tree xpr . xprs)syntax

binds pattern variables of the pattern pat to corresponding items in the tree and executes xpr . xprs in this context. pat and tree needn't be flat, they can be deeply nested pseudo-lists of symbols.

bindable?


(bindable? tree pat . fenders)syntax

tests, if the items in the tree can be bound to pattern variables in pat, which pass the fenders tests.

set-all!


(set-all! pat tree)syntax

sets pattern variables in pat to corresponding items in tree

define-all


(define-all pat tree)syntax

defines pattern variables in pat by setting them to corresponding items in tree

bind-case


(bind-case tree (pat (where . fenders) xpr . xprs) ....)syntax
(bind-case tree (pat xpr . xprs) ....)syntax

Checks if tree matches patterns pat .... in sequence, binds the pattern variables of the first matching pattern to corresponding subexpressions of tree and executes body expressions xpr . xprs in this context

bind-let*


(bind-let* ((pat tree) ...) xpr . xprs)syntax

sequentually binding patterns to trees

bind-let


(bind-let name .. ((pat tree) ...) xpr . xprs)syntax
(bind-let ((pat tree) ...) xpr . xprs)syntax

binding patterns to sequences in parallel, whith or without a recursive name procedure

bind-letrec


(bind-letrec ((pat tree) ...) xpr . xprs)syntax

binding patterns to sequences recursively

bindrec


(bindrec pat tree xpr . xprs)syntax

recursive version of bind

simple-binds


simple-bindsprocedure
simple-binds symprocedure

with sym: documentation of exported symbol without sym: list of exported symbols

Examples

(import simple-binds)

(define (my-map fn lst)
  (bind-case lst (() '()) ((x . xs) (cons (fn x) (my-map fn xs)))))

(let ((x #f) (y #f) (z #f))
  (set-all! x 1)
  (set-all! y 2)
  (set-all! z 3)
  (list x y z))
;-> (quote (1 2 3))

(let ((x #f) (y #f) (z #f)) (set-all! (x y z) '(10 20 30)) (list x y z))
;-> (quote (10 20 30))

(let ((x #f) (y #f) (z #f) (u #f) (v #f))
  (set-all! (x (y . z)) '(1 (2 3 3)))
  (set-all! (u (v)) '(10 (20)))
  (list x y z u v))
;-> (quote (1 2 (3 3) 10 20))

(define-all (x (y . z)) '(1 (2 3 3)))
;-> (if #f #f)

(let ((x #f) (y #f) (z #f)) (set-all! (x (y . z)) '(1 (2 3 3))) (list x y z))
;-> (quote (1 2 (3 3)))

(let ((state #f) (push! #f) (pop! #f))
  (set-all!
    (state (push! pop!))
    (list '()
          (list (lambda (xpr) (set! state (cons xpr state)))
                (lambda () (set! state (cdr state))))))
  (push! 1)
  (push! 0)
  state)
;-> (quote (0 1))

(begin
  (set-all!
    (plus5 times5)
    (let ((a 5)) (list (lambda (x) (+ x a)) (lambda (x) (* x a)))))
  (list (plus5 6) (times5 6)))
;-> (quote (11 30))

(begin (set-all! (x . y) '(1 . 2)) (list x y))
;-> (quote (1 2))

(begin (set-all! (x y . z) '(1 10 . 2)) (list x y z))
;-> (quote (1 10 2))

(begin (set-all! (x (a y (z b))) '(1 (2 3 (4 5)))) (list x y z))
;-> (quote (1 3 4))

(bind-let
  ((pat 'pat))
  (let ((lst '()))
    (set-all!
      (push top pop)
      (list (lambda (xpr) (set! lst (cons xpr lst)))
            (lambda () (car lst))
            (lambda () (set! lst (cdr lst)))))))
;-> (if #f #f)

(begin (push 0) (push 1) (push 2) (pop) (top))
;-> 1

(define-all
  (push top pop)
  (let ((lst '()))
    (list (lambda (xpr) (set! lst (cons xpr lst)))
          (lambda () (car lst))
          (lambda () (set! lst (cdr lst))))))
;-> (if #f #f)

(and (procedure? push) (procedure? top) (procedure? pop))
;-> #t

(begin (push 0) (top))
;-> 0

(begin (push 1) (top))
;-> 1

(begin (push 2) (pop) (top))
;-> 1

(bind a 1 a)
;-> 1

(bind (a b) '(1 2) (list a b))
;-> (quote (1 2))

(bind (x . y) '(1 2 3 4) (list x y))
;-> (quote (1 (2 3 4)))

(bind (x (y . ys) . xs) '(1 (2 3 4) 5 6) (list x y))
;-> (quote (1 2))

(bind (x (y (z . u)) v . w)
      (list 1 (list 2 (cons #f #f)) 5 6)
      (list x y z u v w))
;-> (quote (1 2 #f #f 5 (6)))

(bind (x (y (z . u)) v . w) '(1 (2 (3 . 4)) 5 6) (list x y z u v w))
;-> (quote (1 2 3 4 5 (6)))

(bindrec
  ((o?) e?)
  (list (list (lambda (m) (if (zero? m) #f (e? (- m 1)))))
        (lambda (n) (if (zero? n) #t (o? (- n 1)))))
  (list (o? 95) (e? 95)))
;-> (quote (#t #f))

(bindable? '(1 (2 3)) (a (b c)))
;-> #t

(bindable? 1 a (odd? a))
;-> #t

(bindable? 2 a (odd? a))
;-> #f

(bindable? '(1 (2 3)) (a (b c)) (odd? a))
;-> #t

(bindable? '(1 (2 3)) (a (b c)) (odd? b))
;-> #f

(bindable? '(1 (2 3)) (a (b c)) (odd? a) (odd? c))
;-> #t

(bindable? '(1 (2 3)) (a (b c)) (odd? a) (odd? b) (odd? c))
;-> #f

(bindable? '(1 2 3 4 5) (x y . z) (odd? (car z)))
;-> #t

(bindable? '(1 2 3 4 5) (x (u v) . z))
;-> #f

(bindable? '(1 2 3 4) (x (u v) z))
;-> #f

(bindable? '(2 3 3) (u (a b)))
;-> #f

(bindable? '(1 (2 3 3) 4 5) (x (u (a b)) . z))
;-> #f

(bindable? '(1 (2 3 3) 4) (x (u (a b)) z))
;-> #f

(bindable? 2 u)
;-> #t

(bindable? '(3 3) (a b))
;-> #t

(bindable? '(3 3) (a b) (even? a))
;-> #f

(bindable? '(1 2) ((u v)))
;-> #f

(bindable? '(1 (2 (3 3)) 4) (x (u (a b)) z) (even? a))
;-> #f

(bindable? '(1 (2 (3 3)) 4) (x (u (a b)) z))
;-> #t

(bindable? '((1 2)) ((u v)) (odd? v))
;-> #f

(bindable? '((1 2)) ((u v)))
;-> #t

(bindable? '(2 (3 3)) (u (a b)))
;-> #t

(bind-case '() (() #f))
;-> #f

(bind-case
  '(2 2)
  ((a b) (where (even? a) (odd? b)) (print 'even-odd a b))
  ((a b) (where (odd? a) (even? b)) (print 'odd-even a b))
  ((a b) (list a b)))
;-> (quote (2 2))

(bind-case '(1 (2 3)) ((x (y z w)) #f) ((x (y . z)) (list x y z)) ((x y) #t))
;-> (quote (1 2 (3)))

(bind-case '(1 (2 3)) ((x y) (list x y)) ((x (y . z)) #t) ((x (y z)) #t))
;-> (quote (1 (2 3)))

(bind-case '(1 (2 . 3)) ((x y) (list x y)) ((x (y . z)) #t) ((x (y z)) #f))
;-> (quote (1 (2 . 3)))

(bind-case '(1 2) (() #f) ((a) #f) ((a b) (list a b)) ((a b c) #f))
;-> (quote (1 2))

(set! my-map
  (lambda (fn lst)
    (bind-case lst (() '()) ((x . xs) (cons (fn x) (my-map fn xs))))))
;-> (if #f #f)

(my-map add1 '(0 1 2 3))
;-> (quote (1 2 3 4))

(bind-let
  ((((x y) z) '((1 2) 3)) (u (+ 2 2)) ((v w) '(5 6)))
  (list x y z u v w))
;-> (quote (1 2 3 4 5 6))

(bind-let
  loop
  (((a b) '(5 0)))
  (if (zero? a) (list a b) (loop (list (- a 1) (+ b 1)))))
;-> (quote (0 5))

(bind-let
  loop
  (((x . y) '(1 2 3)) ((z) '(10)))
  (if (zero? z)
    (list x y z)
    (loop (cons (+ x 1) (map add1 y)) (list (- z 1)))))
;-> (quote (11 (12 13) 0))

(bind-let*
  ((((x y) z) '((1 2) 3)) (u (+ 1 2 x)) ((v w) (list (+ z 2) 6)))
  (list x y z u v w))
;-> (quote (1 2 3 4 5 6))

(bind-letrec
  ((o? (lambda (m) (if (zero? m) #f (e? (- m 1)))))
   ((e?) (list (lambda (n) (if (zero? n) #t (o? (- n 1)))))))
  (list (o? 95) (e? 95)))
;-> (quote (#t #f))

(check-all SIMPLE-BINDS (setters?) (binds?) (predicates?) (cases?) (lets?))

Requirements

None

Last update

Mar 23, 2024

Author

Juergen Lorenz

License

Copyright (c) 2022-2024 , Juergen Lorenz, ju (at) jugilo (dot) de 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 disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 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.

Version history

1.1
syntax of bindable? changed
1.0
initial check in

Contents »