2016-07-09 11:23:58 +00:00
|
|
|
;;; -*-scheme-*-
|
2016-05-15 22:07:44 +00:00
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
|
|
|
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
;;;
|
2016-07-09 11:23:58 +00:00
|
|
|
;;; scm.mes: This file is part of Mes.
|
2016-05-15 22:07:44 +00:00
|
|
|
;;;
|
|
|
|
;;; 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/>.
|
|
|
|
|
|
|
|
;; The Maxwell Equations of Software -- John McCarthy page 13
|
|
|
|
;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
|
|
|
|
|
2016-07-11 17:32:11 +00:00
|
|
|
(define (defined? x)
|
|
|
|
(assq x (current-module)))
|
|
|
|
|
2016-07-11 20:16:11 +00:00
|
|
|
(define (procedure? p)
|
|
|
|
(cond ((builtin? p) #t)
|
|
|
|
((pair? p) (eq? (car p) 'lambda))
|
|
|
|
(#t #f)))
|
|
|
|
|
2016-07-09 13:16:30 +00:00
|
|
|
(define (list . rest) rest)
|
2016-07-11 08:38:02 +00:00
|
|
|
(define (vector . rest) (list->vector rest))
|
2016-07-11 09:05:17 +00:00
|
|
|
(define assv assq)
|
|
|
|
(define (memq x lst)
|
|
|
|
(cond ((null? lst) #f)
|
|
|
|
((eq? x (car lst)) lst)
|
|
|
|
(#t (memq x (cdr lst)))))
|
|
|
|
(define memv memq)
|
2016-07-09 13:16:30 +00:00
|
|
|
|
|
|
|
(define-macro (and x y)
|
|
|
|
(cond (x y)
|
|
|
|
(#t #f)))
|
|
|
|
|
|
|
|
(define-macro (or x y)
|
|
|
|
(cond (x x)
|
|
|
|
(#t y)))
|
|
|
|
|
|
|
|
(define (split-params bindings params)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((null? bindings) params)
|
2016-07-09 13:16:30 +00:00
|
|
|
(#t (split-params (cdr bindings)
|
|
|
|
(append params (cons (caar bindings) '()))))))
|
|
|
|
|
|
|
|
(define (split-values bindings values)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((null? bindings) values)
|
2016-07-09 13:16:30 +00:00
|
|
|
(#t (split-values (cdr bindings)
|
|
|
|
(append values (cdar bindings) '())))))
|
|
|
|
|
|
|
|
(define-macro (let bindings . body)
|
|
|
|
(cons (cons 'lambda (cons (split-params bindings '()) body))
|
|
|
|
(split-values bindings '())))
|
2016-07-09 18:12:49 +00:00
|
|
|
|
|
|
|
(define (expand-let* bindings body)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((null? bindings)
|
2016-07-09 18:12:49 +00:00
|
|
|
(cons (cons 'lambda (cons '() body)) '()))
|
|
|
|
(#t
|
|
|
|
(cons
|
|
|
|
(cons 'lambda (cons (cons (caar bindings) '())
|
|
|
|
(cons (expand-let* (cdr bindings) body) '())))
|
|
|
|
(cdar bindings)))))
|
|
|
|
|
|
|
|
(define-macro (let* bindings . body)
|
|
|
|
(expand-let* bindings body))
|
2016-07-09 20:01:13 +00:00
|
|
|
|
|
|
|
(define (map f l . r)
|
2016-07-09 21:12:25 +00:00
|
|
|
(cond ((null? l) '())
|
|
|
|
((null? r) (cons (f (car l)) (map f (cdr l))))
|
|
|
|
((null? (cdr r))
|
2016-07-09 20:05:07 +00:00
|
|
|
(cons (f (car l) (caar r)) (map f (cdr l) (cdar r))))))
|
2016-07-10 11:04:10 +00:00
|
|
|
|
|
|
|
(define (not x)
|
|
|
|
(cond (x #f)
|
|
|
|
(#t #t)))
|
|
|
|
|
|
|
|
(define-macro (if expr then . else)
|
|
|
|
(cond ((not (eq? (c:eval expr (current-module)) #f))
|
|
|
|
then)
|
|
|
|
(#t
|
|
|
|
(cond ((pair? else) (car else))
|
|
|
|
(#t *unspecified*)))))
|
2016-07-10 11:47:56 +00:00
|
|
|
|
|
|
|
(define (unspecified-bindings bindings params)
|
|
|
|
(cond ((null? bindings) params)
|
|
|
|
(#t (unspecified-bindings
|
|
|
|
(cdr bindings)
|
|
|
|
(append params (cons (cons (caar bindings) '(*unspecified*)) '()))))))
|
|
|
|
|
|
|
|
(define (letrec-setters bindings setters)
|
|
|
|
(cond ((null? bindings) setters)
|
|
|
|
(#t (letrec-setters (cdr bindings)
|
|
|
|
(append setters
|
|
|
|
(cons (cons 'set! (car bindings)) '()))))))
|
|
|
|
|
|
|
|
(define-macro (letrec bindings . body)
|
|
|
|
(cons 'let (cons (unspecified-bindings bindings '())
|
|
|
|
(append (letrec-setters bindings '())
|
|
|
|
body))) )
|
2016-07-10 13:19:20 +00:00
|
|
|
|
|
|
|
(define (begin . rest)
|
|
|
|
(let () rest))
|