Phillip Trelford's Array

POKE 36879,255

Turtle Graphics

Over time I’ve been trying to engage my young children with computer programming, so far we’ve looked at Squeak, Small Basic and more recently F#.

They really enjoyed playing with the Turtle in Small Basic, an idea that comes from Logo, a programming language I myself played with as a child.

For i = 1 to 4
  Turtle.Move(100)
  Turtle.TurnRight()
EndFor

Small Basic is a variant of Basic with just 14 keywords. It also ships with a library of static methods for fun stuff like graphics and sound, removing the need to understand the vagaries of threading before getting started.

The SmallBasicLibrary.dll can be also be referenced from C# and F#. However, rather than using value types for passing parameters, it defines it’s own union type called Primitive and associated implicit conversions, a little like the Variant type used in Visual Basic.

I’ve been trying a few different ways of bringing first class Turtle graphics to F#. A few months back I created a simple internal DSL in F# that looks quite a lot like Logo, and can be run in the browser:

repeat 10 [rt 36; repeat 5 [fd 54; rt 72]]
|> Turtle.Run

Just hit the TryFSharp.org button on this Turtle F# Snippet, or download the Silverlight code.

TurtleInternalDSL

More recently based on some of the ideas in the Small Basic Library, I’ve created a small library for C# and F# called Small Sharp, which is hosted on CodePlex.

using Library;

class Program
{
    static void Main(string[] args)
    {
        GraphicsWindow.Show();      
        Turtle.PenName("Red");
        (1000).Times(i =>
        {
            Turtle.Forward(6);
            Turtle.Right(i * 7);
        });
    }
}

Small Sharp provides a fun environment to learn about programming, while giving you the full power of either the C# or F# programming languages. It also includes some LINQ and Ruby like extension methods, for example the sample above runs 1000 times.

TurtleCSharp

Instructions for getting started with C# and F#

Comments are closed