2016-07-25 12:39:56 +00:00
|
|
|
;;; -*-scheme-*-
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
|
|
|
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
;;;
|
|
|
|
;;; base.mes: This file is part of Mes.
|
|
|
|
;;;
|
|
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Mes is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-26 22:32:30 +00:00
|
|
|
(define (identity x) x)
|
|
|
|
(define else #t)
|
|
|
|
|
2016-07-27 06:49:45 +00:00
|
|
|
;;; COND based
|
2016-07-25 12:39:56 +00:00
|
|
|
(define-macro (or . x)
|
2016-07-27 06:49:45 +00:00
|
|
|
(cond ;; COND
|
2016-07-25 12:39:56 +00:00
|
|
|
((null? x) #f)
|
|
|
|
((null? (cdr x)) (car x))
|
2016-07-27 06:49:45 +00:00
|
|
|
(#t (list 'cond (list (car x)) ;; COND
|
2016-07-25 12:39:56 +00:00
|
|
|
(list #t (cons 'or (cdr x)))))))
|
|
|
|
|
2016-07-27 06:49:45 +00:00
|
|
|
(define-macro (and . x)
|
|
|
|
(cond ((null? x) #t) ;; COND
|
|
|
|
((null? (cdr x)) (car x))
|
|
|
|
(#t (list 'cond (list (car x) (cons 'and (cdr x))) ;; COND
|
|
|
|
'(#t #f)))))
|
|
|
|
|
|
|
|
(define (not x)
|
|
|
|
(cond (x #f) ;; COND
|
|
|
|
(#t #t)))
|
2016-07-25 12:39:56 +00:00
|
|
|
|
|
|
|
(define (equal? a b) ;; FIXME: only 2 arg
|
2016-07-27 06:49:45 +00:00
|
|
|
(cond ((and (null? a) (null? b)) #t) ;; COND
|
2016-07-25 12:39:56 +00:00
|
|
|
((and (pair? a) (pair? b))
|
|
|
|
(and (equal? (car a) (car b))
|
|
|
|
(equal? (cdr a) (cdr b))))
|
|
|
|
((and (string? a) (string? b))
|
|
|
|
(eq? (string->symbol a) (string->symbol b)))
|
|
|
|
((and (vector? a) (vector? b))
|
|
|
|
(equal? (vector->list a) (vector->list b)))
|
|
|
|
(#t (eq? a b))))
|
|
|
|
|
|
|
|
(define (memq x lst)
|
2016-07-27 06:49:45 +00:00
|
|
|
(cond ((null? lst) #f) ;; COND
|
2016-07-25 12:39:56 +00:00
|
|
|
((eq? x (car lst)) lst)
|
|
|
|
(#t (memq x (cdr lst)))))
|
|
|
|
|
|
|
|
(define (map f l . r)
|
2016-07-27 06:49:45 +00:00
|
|
|
(cond ((null? l) '()) ;; COND
|
2016-07-25 12:39:56 +00:00
|
|
|
((null? r) (cons (f (car l)) (map f (cdr l))))
|
|
|
|
((null? (cdr r))
|
|
|
|
(cons (f (car l) (caar r)) (map f (cdr l) (cdar r))))))
|
|
|
|
|
2016-07-27 06:49:45 +00:00
|
|
|
;; IF based
|
|
|
|
(define-macro (or . x)
|
|
|
|
(if (null? x) #f ;; IF
|
|
|
|
(if (null? (cdr x)) (car x) ;; IF
|
|
|
|
(list 'if (car x) (car x)
|
|
|
|
(cons* 'or (cdr x))))))
|
|
|
|
|
|
|
|
(define-macro (and . x)
|
|
|
|
(if (null? x) #t ;; IF
|
|
|
|
(if (null? (cdr x)) (car x) ;; IF
|
|
|
|
(list 'if (car x) (cons 'and (cdr x)) ;; IF
|
|
|
|
#f))))
|
|
|
|
|
|
|
|
(define (not x)
|
|
|
|
(if x #f #t))
|
|
|
|
|
|
|
|
(define (equal? a b) ;; FIXME: only 2 arg
|
|
|
|
(if (and (null? a) (null? b)) #t ;; IF
|
|
|
|
(if (and (pair? a) (pair? b))
|
|
|
|
(and (equal? (car a) (car b))
|
|
|
|
(equal? (cdr a) (cdr b)))
|
|
|
|
(if (and (string? a) (string? b)) ;; IF
|
|
|
|
(eq? (string->symbol a) (string->symbol b))
|
|
|
|
(if (and (vector? a) (vector? b)) ;; IF
|
|
|
|
(equal? (vector->list a) (vector->list b))
|
|
|
|
(eq? a b))))))
|
|
|
|
|
|
|
|
(define (memq x lst)
|
|
|
|
(if (null? lst) #f ;; IF
|
|
|
|
(if (eq? x (car lst)) lst ;; IF
|
|
|
|
(memq x (cdr lst)))))
|
|
|
|
|
|
|
|
(define guile? (not (pair? (current-module))))
|
|
|
|
|
|
|
|
(define (map f l . r)
|
|
|
|
(if (null? l) '() ;; IF
|
|
|
|
(if (null? r) (cons (f (car l)) (map f (cdr l))) ;; IF
|
|
|
|
(if (null? (cdr r)) ;; IF
|
|
|
|
(cons (f (car l) (caar r)) (map f (cdr l) (cdar r)))))))
|
|
|
|
|
2016-07-25 12:39:56 +00:00
|
|
|
(define-macro (simple-let bindings . rest)
|
|
|
|
(cons (cons 'lambda (cons (map car bindings) rest))
|
|
|
|
(map cadr bindings)))
|
|
|
|
|
|
|
|
(define-macro (let bindings . rest)
|
|
|
|
(cons* 'simple-let bindings rest))
|