A simple Lisp program

Discussion of Common Lisp
Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: A simple Lisp program

Post by Warren Wilkinson » Wed Dec 29, 2010 12:45 pm

Oh, try this:

Replace this:

Code: Select all

(defun interpret-program (program input)
    (third (interpret-statement-list program (list '(()) input nil))))
With this version:

Code: Select all

(defun interpret-program (program input)
   (destructuring-bind (memory input output) (interpret-statement-list program (list '(()) input nil))
      (format t "~%DONE...~%MEMORY: ~s~%INPUT: ~s~%OUTPUT: ~s" memory input output)))
The new version doesn't change anything, but it does tell you what the programs state was when its finished. That might help you track down the problem. What I think is happening, is that we are binding the variable X to the string "this is a phony string" --- but because interpret-program only returns the output, we don't see that we've successfully made a memory binding.

I think you'll see something like:
MEMORY: ((x . "this is a phony string"))
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

ohbugger
Posts: 10
Joined: Mon Dec 27, 2010 8:56 am

Re: A simple Lisp program

Post by ohbugger » Wed Dec 29, 2010 4:03 pm

So. My TA has responded with the following, in reference to the read statement:

Code: Select all

(defun interpret-read-statement (read-variable state)
    (let ((memory (first state))
           (input (second state)
           (output (third state))
        (list (set-value memory read-variable "this is a phony read value!!!") input output)))

"This is almost correct except value must be extracted from the input list. Then, input list must be adjusted to contain the remaining elements (values)."

and the following BNF grammar for the other two pieces:


if_statement ::=
(if expression then statement_list1 else statement_list2)

For if-statement, you must pass expression, statement_list1, statement_list2 and the state, then evaluate in the interpret-if-statement.
while_statement ::=
(while expression do statement_list)
For while-statement, you must pass expression, statement_list and the state.
- Show quoted text -
So. For starters. would the following be correct for the read statement, making the following assumptions. The command put to the interpreter is:
((interpret-program '(read x) (write x) '(21))

Which would make the "value" portion of "state" the third item in the list. Correct?

Then, in the return statement, we need to remove "value" from state. so it would be a list containing everything except the third value:

Code: Select all

(defun interpret-read-statement (read-variable state)
    (let ((memory (first state))
           (input (second state)
           (output (third state))
           (value (third state))
        (list (set-value memory read-variable (cdddr(state))) input output)))

How does that look?

ohbugger
Posts: 10
Joined: Mon Dec 27, 2010 8:56 am

Re: A simple Lisp program

Post by ohbugger » Wed Dec 29, 2010 4:11 pm

Warren Wilkinson wrote:Oh, try this:

Replace this:

Code: Select all

(defun interpret-program (program input)
    (third (interpret-statement-list program (list '(()) input nil))))
With this version:

Code: Select all

(defun interpret-program (program input)
   (destructuring-bind (memory input output) (interpret-statement-list program (list '(()) input nil))
      (format t "~%DONE...~%MEMORY: ~s~%INPUT: ~s~%OUTPUT: ~s" memory input output)))
The new version doesn't change anything, but it does tell you what the programs state was when its finished. That might help you track down the problem. What I think is happening, is that we are binding the variable X to the string "this is a phony string" --- but because interpret-program only returns the output, we don't see that we've successfully made a memory binding.

I think you'll see something like:
MEMORY: ((x . "this is a phony string"))
Tried this. No dice :(
Still getting no output. Grumble.

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: A simple Lisp program

Post by Warren Wilkinson » Thu Dec 30, 2010 12:48 pm

How does that look?
It would give you a compilation error because (state) isn't a function [hint: it's a variable].

Heres what I want you to do. Add, as the first line, this line of code to every function:

Code: Select all

(format t "~%Function: xxx")
Replace xxx with the name of the function. Then when you run your program, you will see where its going (and help you figure out what it is really doing). Please include that output in your next post.
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

ohbugger
Posts: 10
Joined: Mon Dec 27, 2010 8:56 am

Re: A simple Lisp program

Post by ohbugger » Thu Dec 30, 2010 1:18 pm

Sir, part of the problem I have is the interpreter only gives errors for unmatched parens. That's it. I'm using Xlisp-plus 3.04. If you have suggestions for some other interpreter, that would be wonderful. The project is due tonight, unfortunately. Trying your code now, will report back.

Still nothing. Does Lisp have any kind of IDE available anywhere?

Warren Wilkinson
Posts: 117
Joined: Tue Aug 10, 2010 11:24 pm
Location: Calgary, Alberta
Contact:

Re: A simple Lisp program

Post by Warren Wilkinson » Thu Dec 30, 2010 2:01 pm

According to the Xlisp documentation, (format t "a string") should print the string. The reason I had you add format statements everywhere was so that we could trace the flow of execution. If XPlus isn't printing the format statements, and you don't know why, you should probably seek face time with your TA.
Need an online wiki database? My Lisp startup http://www.formlis.com combines a wiki with forms and reports.

ohbugger
Posts: 10
Joined: Mon Dec 27, 2010 8:56 am

Re: A simple Lisp program

Post by ohbugger » Thu Dec 30, 2010 3:44 pm

TA doesn't give enough of a shit to do such things. You guys were my last hope. Thanks for trying though.

Post Reply