Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have written a LISP interpreter in PURE LISP. Unfortunately, my code is not able to process these instructions/ tests: CANNOT CHANGE my-eval as it

I have written a LISP interpreter in PURE LISP. Unfortunately, my code is not able to process these instructions/ tests: CANNOT CHANGE my-eval as it is a given for the assignment.

FAILING TESTS: (cons 'foo '(a b c))

(print '(a b c))

;; giving symbols a value and evaluation of symbols

(setq a '(a b c)) a

;; cond - the conditional expression

(cond (nil 2 3 4 5 1) (t 1 3 4 2) (t 1 3 4 5 3))

(cond ((eq t nil) 1) ((eq t t) 2)(t 3)) ;; defining a function and calling that function

(defun rev (L R) (cond ((null L) R) (t (rev (cdr L) (cons (car L) R)))))

(rev a nil)

(rev (rev a nil) nil)

;; define and call another function in nested combination

(defun app (L R)(cond ((null L) R)(t (cons (car L) (app (cdr L) R)))))

(app (app a a) (app a a))

MY CODE:

(defun my-assoc (v alist) (cond ((null alist) nil) ((eq (car (car alist)) v) (car alist)) (t (my-assoc v (cdr alist)))) )

(defun my-eval (e alist) (cond ((atom e) (my-eval-atom e alist)) (t (my-apply (car e) (cdr e) alist)) ) )

(defun my-eval-atom (e alist) (cond ((eq e 't) t) ((eq e 'nil) nil) ((listp e) e) ; handle quoted lists ((eq (type-of e) 'symbol) (cdr (my-assoc e alist))) (t e)))

(defun my-apply (fn args alist) (cond ((atom fn) (my-apply-atom fn args alist)) ( t (my-apply-lambda fn args alist))) )

(defun my-apply-atom (fn args alist) (cond ((eq fn 'cons) (cons (my-eval (car args) alist) (my-eval (cdr args) alist))) ((eq fn 'car) (car (my-eval (car args) alist))) ((eq fn 'cdr) (cdr (my-eval (car args) alist))) ((eq fn 'eq) (eq (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'quote) (car args)) ((eq fn 'null) (null (my-eval (car args) alist))) ((eq fn 'atom) (atom (my-eval (car args) alist))) ((eq fn 'listp) (listp (my-eval (car args) alist))) ((eq fn 'cond) (my-eval-cond (cdr args) alist)) ((eq fn 'eval) (my-eval (my-eval (car args) alist) alist)) ((eq fn 'print) (progn (print (my-eval (car args) alist)) nil)) ((eq fn 'defun) (my-eval-defun args alist)) ((eq fn 'setq) (let ((var (car args)) (val (my-eval (cadr args) alist))) (setf (cdr (my-assoc var alist)) val) val)) (t (my-apply-lambda (my-assoc fn alist) args alist)))) (defun my-apply-lambda (lambda-form args alist) (let ((formals (cadr lambda-form)) (body (cddr lambda-form))) (my-eval-list (my-bind-formals formals args alist) body alist))) (defun my-bind-formals (formals args alist) (if (null formals) alist (let ((eval-arg (my-eval (car args) alist))) (my-bind-formals (cdr formals) (cdr args) (cons (cons (car formals) eval-arg) alist))))) (defun my-eval-cond (clauses alist) (if (null clauses) nil (let ((clause (car clauses))) (if (my-eval (car clause) alist) (my-eval-list (cdr clause) alist) (my-eval-cond (cdr clauses) alist))))) (defun my-eval-defun (form alist) (let ((fn-name (car form)) (fn-args (cadr form)) (fn-body (cddr form))) (setq global-alist (my-assoc fn-name global-alist '())) (setf (cdr (my-assoc fn-name global-alist)) `(lambda ,fn-args ,@fn-body)) fn-name)) (defun my-eval-setq (var val alist) (let ((var-cell (my-assoc var alist))) (if var-cell (setf (cdr var-cell) (my-eval val alist)) (setq alist (cons (cons var (my-eval val alist)) alist))) (my-eval val alist))) (defvar global-alist nil)

(defun my-top () (prog () top ;; read an s-expression, evaluate it using my-eval passing in the global-alist, ;; then print the result, functions and global variables will be on global-alist

(print (my-eval (read) global-alist)) (terpri) ;; prints a newline (go top) ;; loops forever ) )

(defun my-eval-list (exp-list alist) (if (null (cdr exp-list)) (my-eval (car exp-list) alist) (progn (my-eval (car exp-list) alist) (my-eval-list (cdr exp-list) alist)))) Can you please fix it and share the new code with successful passing of all tests. All tests are:

(my-top) ;; your REPL using my-eval to evaluate symbolic expressions

;; atoms t nil "Hello" 10

;; primitive functions (eq t t) (eq nil nil) (eq t nil)

(null nil)

(null t)

(quote (a b c)) same as '(a b c) (eq 'a 'a) (eq '(a b) '(a b)) ; should be nil (car '(a b c)) (cdr '(a b c)) (cons 'foo '(a b c))

(print '(a b c))

;; giving symbols a value and evaluation of symbols

(setq a '(a b c)) a

;; cond - the conditional expression

(cond (nil 2 3 4 5 1) (t 1 3 4 2) (t 1 3 4 5 3))

(cond ((eq t nil) 1) ((eq t t) 2)(t 3)) ;; defining a function and calling that function

(defun rev (L R) (cond ((null L) R) (t (rev (cdr L) (cons (car L) R)))))

(rev a nil)

(rev (rev a nil) nil)

;; define and call another function in nested combination

(defun app (L R)(cond ((null L) R)(t (cons (car L) (app (cdr L) R)))))

(app (app a a) (app a a))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Factory Active Database For Enterprise Computing

Authors: Schur, Stephen

1st Edition

0471558443, 9780471558446

More Books

Students also viewed these Databases questions

Question

2. Outline the functions of nonverbal communication

Answered: 1 week ago