Sunday, March 1, 2009

Logo list evaluation

Here's a quick update; the problem that I'm considering right now.

Consider the following code in logo:

? print first [3+5]
? print if 1=1 [3+5]

For the first statement, the output is:
3+5

For the second statement, the output is:
8

Why is this significant to logo? Well, it turns out these cases represent special cases in UCBLogo for handling input inside a list. The list [3+5] is a list with one word, '3+5'. If you do the command first [3+5], the word '3+5' is returned. However, if you do an evaluation of the list [3+5] (i.e. if 1=1 [3+5]), the list is interpreted as having 3 symbols, "3", "+", and "5", and is evaluated as 8.

The solution to this problem looks like it's simple enough: If you evaluate an instruction list, and you come across a word which has the symbol(s) +, - (with the exception if it's the first symbol of the word), /, <, (, ), =, or > then you split up the word into separate words around these delimiters. You then evaluate the list.

Fun stuff!

No comments: