From 4ff96673c732c5fd0f3b936f41387988a98ec1f1 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 18 Oct 2016 08:24:47 +0200 Subject: [PATCH] Avoid cons* in base. * module/mes/base-0.mes (cond, simple-let, let): Rewrite without cons*. * module/mes/base.mes (or): Likewise. --- module/mes/base-0.mes | 33 +++++++++++++++++++-------------- module/mes/base.mes | 2 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/module/mes/base-0.mes b/module/mes/base-0.mes index 492e2eff..e34b9693 100644 --- a/module/mes/base-0.mes +++ b/module/mes/base-0.mes @@ -51,26 +51,31 @@ (cons (car rest) (loop (cdr rest))))) (loop (cons x rest))) -(define-macro cond - (lambda clauses - (if (null? clauses) *unspecified* +(define-macro (cond . clauses) + (list 'if (null? clauses) *unspecified* (if (null? (cdr clauses)) - (list 'if (car (car clauses)) - (cons* 'begin (car (car clauses)) (cdr (car clauses))) - *unspecified*) - (if (eq? (car (cadr clauses)) 'else) - (list 'if (car (car clauses)) - (cons* 'begin (car (car clauses)) (cdr (car clauses))) - (cons* 'begin *unspecified* (cdr (cadr clauses)))) - (list 'if (car (car clauses)) - (cons* 'begin (car (car clauses)) (cdr (car clauses))) - (cons* 'cond (cdr clauses)))))))) + (list 'if (car (car clauses)) + (list (cons 'lambda (cons '() (cons (car (car clauses)) (cdr (car clauses)))))) + *unspecified*) + (if (eq? (car (cadr clauses)) 'else) + (list 'if (car (car clauses)) + (list (cons 'lambda (cons '() (car clauses)))) + (list (cons 'lambda (cons '() (cons *unspecified* (cdr (cadr clauses))))))) + (list 'if (car (car clauses)) + (list (cons 'lambda (cons '() (car clauses)))) + (cons 'cond (cdr clauses))))))) (define else #t) +(define (map f l . r) + (if (null? l) '() + (if (null? r) (cons (f (car l)) (map f (cdr l))) + (if (null? (cdr r)) + (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)) + (cons 'simple-let (cons bindings rest))) diff --git a/module/mes/base.mes b/module/mes/base.mes index 41f6247d..938cdb88 100644 --- a/module/mes/base.mes +++ b/module/mes/base.mes @@ -35,7 +35,7 @@ (if (null? x) #f (if (null? (cdr x)) (car x) (list 'if (car x) (car x) - (cons* 'or (cdr x)))))) + (cons 'or (cdr x)))))) (define-macro (and . x) (if (null? x) #t