From 1654d2e6b238f2c458dbc63391bc7384f70df727 Mon Sep 17 00:00:00 2001 From: zaoqi Date: Sat, 7 Oct 2017 20:06:19 +0800 Subject: [PATCH] Fix --- mal.rkt | 2 +- scm.rkt | 2 +- zscm.rkt | 46 ++++++++++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/mal.rkt b/mal.rkt index 91753d2..7de0584 100644 --- a/mal.rkt +++ b/mal.rkt @@ -239,4 +239,4 @@ (swap! s (unfunc f)))) )) -(displayln (cons 'do (c (read)))) +(writeln (cons 'do (c (read)))) diff --git a/scm.rkt b/scm.rkt index fa0a58d..2e50ef8 100644 --- a/scm.rkt +++ b/scm.rkt @@ -107,4 +107,4 @@ (cdr x) (error "unbegin"))) -(displayln (cons 'begin (c (read)))) +(writeln (cons 'begin (c (read)))) diff --git a/zscm.rkt b/zscm.rkt index 600607f..58892e3 100644 --- a/zscm.rkt +++ b/zscm.rkt @@ -241,7 +241,7 @@ (define-syntax %%define-record-type%c%% (syntax-rules () [(_ (f a)) f] - [(_ (f a setv)) (atom f)])) + [(_ (f a setv)) (atom! f)])) (define-syntax %define-record-type (syntax-rules () [(_ name pred x) (void)] @@ -272,12 +272,26 @@ (%vector-ref v k) (error "vector-ref: isn't vector?" x))) - (define-record-type delay-v - (%delay-v lazy) - promise? - (lazy %lazydelay-vv)) - (define-syntax-rule (delay x) (%delay-v (lambda () x))) - (define (force x) ((%lazydelay-vv x))) ;Fix: memorize + ,@(if (has-feature? 'atom) + '((define-record-type delay-v + (%delay-v lazy) + promise? + (lazy %lazydelay-vv %set%delay!)) + (define-syntax-rule (delay x) (%delay-v (lambda () x))) + (define (force x) + (let ([v (%lazydelay-vv x)]) + (if (procedure? v) + (let ([f (v)]) + (begin + (%set%delay! x (list f)) + f)) + (car v))))) + '((define-record-type delay-v + (%delay-v lazy) + promise? + (lazy %lazydelay-vv)) + (define-syntax-rule (delay x) (%delay-v (lambda () x))) + (define (force x) ((%lazydelay-vv x))))) (define-syntax-rule (delay-force x) (delay (force x))) (define (make-promise x) (if (promise? x) x (delay x))) @@ -326,9 +340,9 @@ (define call-with-current-continuation call/cc) (define (%reverse xs rs) - (if (null? xs) - rs - (%reverse (cdr xs) (cons (car xs) rs)))) + (if (null? xs) + rs + (%reverse (cdr xs) (cons (car xs) rs)))) (define (reverse xs) (%reverse xs '())) (define (member x xs) @@ -383,12 +397,12 @@ hash? (xs hash->list)) (define (%make-immutable-hash-loop rs xs) - (if (null? xs) - (%make-immutable-hash rs) - (let* ([x (car xs)] [xa (car xs)]) - (if (ormap (λ (y) (equal? (car y) xa)) rs) - (%make-immutable-hash-loop rs (cdr xs)) - (%make-immutable-hash-loop (cons x rs) (cdr xs)))))) + (if (null? xs) + (%make-immutable-hash rs) + (let* ([x (car xs)] [xa (car xs)]) + (if (ormap (λ (y) (equal? (car y) xa)) rs) + (%make-immutable-hash-loop rs (cdr xs)) + (%make-immutable-hash-loop (cons x rs) (cdr xs)))))) (define (make-immutable-hash xs) (%make-immutable-hash-loop '() (reverse xs))) (define (hash-set hash key v)