August 19, 2013

Starting with Turtlebits

Three little examples of Coffeescript and the turtlebits functions.

The basic turtle motion functions are the same as LOGO: "fd 100" moves forward 100 pixels; "bk 100" moves back 100 pixels; "rt 90" turns right 90 degrees and "lt 90" turns left 90 degrees.

More below...


Turtle Speed

When moving the turtle, the program actually queues up the motion as an animation so that you can see it make one move each second. The speed of the turtle can be changed. For example, "speed 10" will make the turtle move 10 times faster than its normal leisurely pace. To turn off animation, "speed Infinity" will have the turtle move instantly without queuing.

Trace lines by specifying a pen color: "pen red" traces in red. The empty pen can be selected using "pen null". (Any CSS colorname can be specified as a pen color by specifying a string.)

Coffeescript is an indent-sensitive language, which means that loops and function definitions are indicated by indenting the contents of the loop or definition.

Here is a loop (click on the link to try it):

# draw a star
speed 10
pen gold
for x in [1..5]
  fd 100
  rt 144

In Coffeescript, lines that start with a # symbol are comments that are ignored when running the program.

Functions

In Coffeescript, functions are specified using an arrow: "(parameters) -> stuff-to-do". If there is more than one line of stuff to do, just indent it underneath. Here is a program that defines a function called "polygon" (click on the link to try the program):

# draws polygons
polygon =
  (sides, perimeter) ->
    for x in [1..sides]
      fd perimeter / sides
      rt 360 / sides
speed 10
for n in [3..6]
  lt 90
  pen orange
  polygon n, 100
  pen null
  rt 90
  fd 50

Notice that the function definition does not include the line "speed 10" because that line is not indented. After the function "polygon" is defined, it is used in the loop by running "polygon n, 100".

Coordinate Motion

Sometimes it is useful to use Cartesian coordinates. The function "moveto x, y" moves to the pixel at x, y (where 0, 0 is defined as the center of the screen, and positive offsets go up and to the right, as you would see in math class). There is another function "jumpto", that does the same thing, but without tracing a line.

The following program uses moveto and jumpto to draw some graphs:

# draws sine and abs
speed 100
graph = (fn) ->
  range = 2
  scale = 100
  jumpto -scale * range, fn(-range) * scale
  for x in [-scale * range ... scale * range]
    y = fn(x / scale) * scale
    moveto x, y
pen red
graph (x) -> Math.cos(x)
pen blue
graph (x) -> Math.abs(x)

This program is worth exploring a bit. Again, click on the link to try it out.

Functions as First-Class Objects

There is a more advanced concept used in the program above. The "fn" argument to the function "graph" is itself a function.

Notice that the statement "graph (x) -> Math.sin(x)" does not define graph: the graph function is already defined at the top.

What it is doing is it is calling the graph function, and telling it to operate on the function "(x) -> Math.sin(x)". The graph function takes this function parameter and calls it "fn". Then within the loop inside "graph", it calls fn repeatedly to compute the coordinates.

Thoughts on Notation

The concept of a function as a first-class object is a key insight in math. You need to understand that a function itself is a thing before you can understand that you might want to find its zeroes, or its derivative.

Coffeescript uses a very succinct and math-like notation for a function that can be easily passed around. Because of this, Coffeescript is a good language for writing little programs that draw graphs or that do numerical integration, root finding, and so on.

Functions an example of why notation is important, and why the "No Limits" goal of a learning programming language is important. Some languages do not have the concept of a function as a first-class object; or some that do impose an unwieldy jumble of boilerplate syntax to express it. A learning language should invite you to explore the important concepts without wrestling with plumbing. The language should not get in your way.

Coffeescript has good notation for the most important programming concepts: algebraic expressions, loops, functions, arrays, objects, and strings. The right notation can simplify learning.

Posted by David at August 19, 2013 02:44 AM
Comments
Post a comment









Remember personal info?