Lecture 20 - November 1, 2007
We went over two pages on the exam:
the one on type, declaration and structural equivalenced
the one on bindings: name, value, type, address.
Returned remaining Snobol programs
Returned first project submission.
In class quiz is shown below along with two additional code examples showing that Snobol does NOT print an error message when you
access an out of bounds array element.
* filename: quiz.sno -- in class quiz - November 1, 2007 &TRIM = 1 A = ARRAY(INPUT) I = 1 READ A<I> = INPUT :F(PRINT) I = I + 1 :(READ) PRINT I = I - 1 OUTPUT = A<I> :S(PRINT) END |
* filename: quizzie.sno * illustrates that Snobol provides no information about an out of bounds * array index (see red code) &TRIM = 1 A = ARRAY(INPUT) I = 1 READ A<I> =
INPUT :F(PRINT) I = I + 1 :(READ) PRINT I = I - 1 OUTPUT = A<I> :S(PRINT) output = A<300> END |
– this file illustrates how to force Snobol to tell you if you go out of bounds
* - code in red shows how to do it
* filename: quizzie2.sno * illustrates how to force Snobol to tell you if you go out of bounds * (see red code) &TRIM = 1 A = ARRAY(INPUT) I = 1 READ A<I> = INPUT :F(PRINT) I = I + 1 :(READ) PRINT I = I - 1 OUTPUT = A<I> :S(PRINT) output =
a<300> :f(err) s(end) err output = ' could not print a<300>' END |
We then talked about some additional Snobol features:
the TABLE storage structure and
the DATA.primitive function
Snobol has an additional data structure known as a TABLE which is similar to an ARRAY except that ARRAY indexes are required to
be integers while TABLE indexes are not.
The primitive function DATA can be used to define the data type NODE with the two field functions, VALUE and LINK. This is useful if you want to make a simple linear linked list made up of nodes, each containing a value field and a link field.
DATA('NODE(VALUE,LINK'))
P = NODE('S') creates a node with value field S and the null string in the link field
The fields can be reference by VALUE(P) and LINK(P)
P = NODE('T',P) puts node T at the head of the list
P = LINK(P) deletes a node from the head of the list
We played around with the DATA function.
NOTE: in our example we wrote very repetitive code to print out the contents of the list. It should be replaced by a loop once we
have written enough repetitive code to know what the exit condition is.
In the box below is some of this code:
* filename node.sno
DATA('NODE(VALUE,LINK)')
P = NODE('S')
OUTPUT = VALUE(P)
P = NODE('T',P)
OUTPUT = VALUE(P)
P = NODE('K',P)
OUTPUT = VALUE(P)
M = NODE('L')
LINK(M) = P
P = M
OUTPUT = " value in P is " VALUE(P)
OUTPUT = " value in M is " VALUE(M) OUTPUT = " Here are all of the value in the list from head to the end " HEAD = P OUTPUT = VALUE(P) P = LINK(P) OUTPUT = VALUE(P) P = LINK(P) OUTPUT = VALUE(P) P = LINK(P) OUTPUT = VALUE (P)
END |
Below is the problem that is to be submitted by 11:59 Monday night to Blackboard.
Let a binary tree be a data structure composed of nodes that contain three fields: value, left son, right son. A tree has a distinguished node, the root, that is not the left son or right son of any node.