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.

PLEASE MAKE SURE YOU HAVE FIXED THE CODE AND DO NOT GUESS RANDOMLY

FAILING TESTS:

(setq a '(a b c))

(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 'car) (car (my-eval (car args) alist))) ((eq fn 'cdr) (cdr (my-eval (car args) alist))) ((eq fn 'cons) (cons (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'eq) (eq (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'quote) (car args)) ((eq fn 'setq) (my-eval-setq (car args) (my-eval (cadr args alist)) alist)) ((eq fn 'defun) (my-eval-defun args alist)) ((eq fn 'cond) (my-eval-cond args alist)) ((eq fn 'eval) (my-eval (my-eval (car args) alist) alist)) ((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 'apply) (apply (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'equal) (equal (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn '+ ) (+ (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn '- ) (- (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'mod ) (mod (my-eval (car args) alist) (my-eval (cadr args) alist))) ((eq fn 'floor ) (floor (my-eval (car args) alist) (my-eval (cadr args) alist))) (t (my-apply nil (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))))

(defun my-eval-list (lst alist) (if (null lst) nil (let ((last-result (my-eval (car lst) alist))) (if (cdr lst) (my-eval-list (cdr lst) alist) last-result)))) PASSING TESTS:

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))

 

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

More Books

Students also viewed these Databases questions