mes/base.mes

110 lines
3.4 KiB
Scheme

;;; -*-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/>.
(define (identity x) x)
(define else #t)
;;; COND based
(define-macro (or . x)
(cond ;; COND
((null? x) #f)
((null? (cdr x)) (car x))
(#t (list 'cond (list (car x)) ;; COND
(list #t (cons 'or (cdr x)))))))
(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)))
(define (equal? a b) ;; FIXME: only 2 arg
(cond ((and (null? a) (null? b)) #t) ;; COND
((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)
(cond ((null? lst) #f) ;; COND
((eq? x (car lst)) lst)
(#t (memq x (cdr lst)))))
(define (map f l . r)
(cond ((null? l) '()) ;; COND
((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))))))
;; 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)))))))
(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))