Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


A LISP question

A LISP question

A LISP question

So i'm trying to get this Lisp program running. Unfortunately, I can't even get the read statement to spit out anything meaningful, so i can't really find a way to debug using xlisp32.  A working statement, for example would be:  ((interpret-program '(read x) (write x) '(21)).  Even when I try these statements as unary statements though, I don't even get a (nil) It just doesn't respond.  

Here's the code i've got so far:

; main function creates state containing supplied input and empty
; memory and output, returns output

(defun interpret-program (program input)
    (third (interpret-statement-list program (list '(()) input nil))))

; interprets a single statement if statement-list is a single statement
; recursively interprets all statements if it is a list

(defun interpret-statement-list (statement-list state)
        ((null statement-list) state)
        ((atom (first statement-list))
            (interpret-statement statement-list state))
        (t (interpret-statement-list (rest statement-list)
            (interpret-statement (first statement-list) state)))))

; determines the type of statement based on the first word
; and calls the appropriate function to interpret it

(defun interpret-statement (statement state)
        ((eq (first statement) 'assign)
            (interpret-assignment-statement (second statement)
            (fourth statement) state))
        ((eq (first statement) 'write)
            (interpret-write-statement (second statement) state))
    ((eq (first statement) 'read)
        (interpret-read-statement (second statement) state)))
    ((eq (first statement) 'if)
        (interpret-if-statement (second statement) state))
    ((eq (first statement) 'while)
        (interpret-while-statement (second statement) state)))

; interprets the write statement by evaluating the expression
; and appending its value to the output stream

(defun interpret-write-statement (write-expression state)
        ((memory (first state))
        (input (second state))
        (output (third state)))
    (list memory input (append output
        (list (evaluate-expression write-expression state))))))

;interprets the read statement by removing the first item from the list
; and putting it into a variable.

(defun interpret-read-statement (read-expression state)
        ((memory (first state))
        (input (second state)
        (output (third state))
         (set-value (memory) (input) (output))
        (list (evaluate-expression read-expression state)))))

; interprets the assignment statement by evaluating the expression
; and assigning the value to the specified variable

(defun interpret-assignment-statement (variable expression state)
        ((memory (first state))
        (input (second state))
        (output (third state)))
    (list (set-value memory variable
        (evaluate-expression expression state)) input output)))

; evaluates an expression by checking whether it is a literal, a
; variable, a binary or unary expression

(defun evaluate-expression (expression state)
        ((numberp expression) expression)
        ((atom expression) (get-value (first state) expression))
        ((eq (length expression) 3)
            (evaluate-binary-expression expression state))))

; evaluates a binary expression by first evaluating the subexpressions
; and then applying the operator to those subexpressions

(defun evaluate-binary-expression (expression state)
        ((left-operand (evaluate-expression (first expression) state))
        (operator (second expression))
        (right-operand (evaluate-expression (third expression) state)))
    (apply operator (list left-operand right-operand))))

; adds the variable-value pair to the head of the association list
; that comprises the current state of the memory.  Previous
; occurences of that variable are not removed.

(defun set-value (memory variable value)
    (cons (list variable value) memory))

; retrieves the value of a variable from the association list
; that comprises the current state of the memory

(defun get-value (memory variable)
    (second (assoc variable memory)))

;handles if statements
;if the expression is null, then its false, so skip it
;if the expression is true, then we do whatever is in state
;the final statement must evaluate to true
(defun interpret-if-statement (expression state)
            ((null statement-list) state)
        (expression (statement-list) state)
        ((t first state))
;handles while statements.  all it does is evaluate.  If its true, we've reached the condition
;and so we stop. Otherwise, recursively evaluate itself again, until we reach a false value.  Uses
;simple tail recursion.

(defun interpret-if-statement (expression state)
            ((null statement-list) state)
        (expression state)
        (interpret-if-statement(expression state))
        (expression (statement-list) state)

Any help and pointers would be very useful. I'm completely new to LISP, so please treat me as a complete newb for functional languages. Thank you in advance.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close