3 ms·
One of the advantages of TCE in Logo (in combination with turtle graphics) is that you can teach the concept of recursion in a gradual and intuitive manner. You
by RodgerTheGreat 5y ago
One of the advantages of TCE in Logo (in combination with turtle graphics) is that you can teach the concept of recursion in a gradual and intuitive manner. You can start with drawing a circle, using recursion to make an infinite loop, stopping it manually after a while:
to circle
fd 3
rt 7
circle
end
Add a parameter, and get a spiral. The turtle winds its way off the screen, and again the student can stop the program manually:
to spiral :x
fd :x
rt 30
spiral (:x + 1)
end
Introduce a base-case, and now the program stops itself at a desired size:
to spiral :x
if (:x = 30) [ stop ]
fd :x
rt 30
spiral (:x + 1)
end
Small, unsurprising steps building up an intuitive grasp of a useful concept.
TCE can also improve the economy of the language, reducing the number of "primitive" control flow concepts that need to be provided out of the box. Since Logo has no "special forms" and is not eagerly evaluated like a Lisp, it's easy to make new control constructs without needing to teach any magic:
to while :cond :proc
unless run :cond [ stop ]
run :proc
while :cond :proc
end
Little things matter.
- xapata 5y agoTurtle in Python would have the same flow of ideas up to the invention of new control flow, which I agree is cool. But that's why Python isn't a LISP. Yet, the creation of a "while" here isn't so different than making a new function which performs that logic. Was your example intended to demonstrate creating new syntax?
- RodgerTheGreat 5y agoYes, it's a do-while loop. It takes two arguments ("cond" and "proc") which would both be bracketed lists. "unless" is common sugar for a negated if statement. "run" evaluates a list as an expression. You'll notice that if/unless also take their bodies as lists, rather than any sort of special-purpose block syntax. "stop" short-circuits the current procedure and immediately returns. Since Logo has dynamic scope, any variable references in "cond" and "proc" will resolve the same within "while" as they did in its caller; no nasty visibility surprises. Since Logo does not eagerly evaluate lists, there is no need to "quote" or otherwise escape the lists in the caller's context. It is precisely that one can define such a construct without defining new syntax per se that is useful.
- xapata 5y ago> there is no need to "quote" or otherwise escape the lists in the caller's context. That's a handy feature. Thanks for the explanation.