From 86012f579bd4e90bcb08e5d4c4bca23ff9759f3c Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 8 Nov 2020 15:46:59 +0530 Subject: add define lambda shorthand --- environment.scm | 3 +-- main.scm | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/environment.scm b/environment.scm index 37f9fa4..d5e606d 100644 --- a/environment.scm +++ b/environment.scm @@ -29,7 +29,7 @@ (error "Too few or too many arguments supplied" vars vals))) ;;; variable lookup -(define (is-unassigned? u) (eq? u '*unassigned*)) +(define (is-unassigned? u) (eq? u ''*unassigned*)) (define (lookup-variable-value var env) (cond [(empty-env? env) (error "Unbound variable" var)] @@ -77,6 +77,5 @@ the-empty-env)]) (define-variable! 'true #t initial-env) (define-variable! 'false #f initial-env) - (define-variable! '*unassigned* #f initial-env) initial-env)) diff --git a/main.scm b/main.scm index 1781255..bc7c862 100644 --- a/main.scm +++ b/main.scm @@ -71,6 +71,17 @@ (define (definition? expr) (tagged? expr 'define)) (define (definition-name expr) (cadr expr)) (define (definition-value expr) (caddr expr)) +(define (definition-lambda? expr) (pair? (definition-name expr))) +(define (definition-lambda-name expr) (caadr expr)) +(define (definition-lambda-params expr) (cdadr expr)) +(define (definition-lambda-body expr) (cddr expr)) +(define (make-definition name value) (list 'define name value)) +(define (definition->lambda expr) + (let ([name (definition-lambda-name expr)] + [params (definition-lambda-params expr)] + [body (definition-lambda-body expr)]) + (make-definition name + (make-lambda params body)))) ;;; lambda (define (lambda-form? expr) (tagged? expr 'lambda)) @@ -134,7 +145,7 @@ statements)) (define (make-unassigned-bindings internal-defines) (map (lambda (d) - (list (definition-name d) '*unassigned*)) internal-defines)) + (list (definition-name d) ''*unassigned*)) internal-defines)) (define (scan-out-defines body) (let ([internal-defines (filter definition? body)] [rest (filter (lambda (n) (not (definition? n))) body)]) @@ -197,10 +208,12 @@ env)) (define (eval-definition expr env) - (define-variable! - (definition-name expr) - (eval-expr (definition-value expr) env) - env)) + (cond + [(definition-lambda? expr) (eval-expr (definition->lambda expr) env)] + [else (define-variable! + (definition-name expr) + (eval-expr (definition-value expr) env) + env)])) (define (eval-function-application expr env) (let ([proc (operator expr)] -- cgit v1.2.3