Phillip Trelford's Array

POKE 36879,255

Try 10 Programming Languages in 10 minutes

There are a lot of interesting programming languages out there, but downloading and setting up the environment can be very time consuming when you just want to try one out. The good news is that you can try out many languages in your browser straight away, often with tutorials which guide you through the basics.

Following the pattern of 7 languages in 7 weeks book, here’s a somewhat abridged version.

Dynamic Languages

Fed up of long compile times, want a lightweight environment for scripting? Dynamic languages could be your new friend.

Try Lua

Lua is a lightweight dynamic language with excellent coroutine support and a simple C API making it hugely popular in video gaming for scripting. Have fun with game engines like LÖVE and Marmalade Quick.

Try Clojure

Clojure is the brainchild of the hugely charismatic speaker Rich Hickey, it is a descendant of one of the earliest programming languages LISP. There’s a really rich community around Clojure, one of my favourite projects is Sam Aaron’s Overtone live coding audio environment.

Try R (quick registration required)

R is a free environment for statistical computing and graphics, with a huge range of user-submitted packages. Ever wondered how to draw an egg?

Functional Languages

Aspects of functional programming have permeated most mainstream languages from C++ to VB. However to really appreciate the expressiveness of the functional approach a functional-first language is required.

Try Erlang

Erlang is a really interesting language for building fault tolerant concurrent systems. It also has great pattern matching capabilities. It has many industrial applications and tools including the RabbitMQ messaging system and the distribute database Riak.

Try Haskell

Haskell is heavily based on the Miranda programming language which was taught in British universities in the 80s and 90s. Haskell added Monads and Type Classes, and is still taught in a few universities, it is also still quite popular in academic research.

Try OCaml

OCaml like Miranda is based on the ML programming language adding object-oriented constructs. F# is based on OCaml, there is even a compatibility mode. OCaml still has industrial application, for example at Jane Street Capital and XenSource.

Web Languages

There’s a plethora of languages that compile to JavaScript languages out there. Also worth a look are the new features in JavaScript itself, see Brendan Eich’s talk at Strangeloop last year on the The State of JavaScript. Here’s 3 *Script languages I find particularly interesting:

LiveScript

LiveScript is an indirect descendant of CoffeeScript with features to assist functional programming like pattern matching and function composition. Check out 10 LiveScript one liners to impress your friends.

Try Elm

Elm is a functional reactive language for creating highly interactive programs, including games. Reactive programming is an interesting direction and I think languages designed specifically for this are worth investigating.

PogoScript

Unfortunately there’s currently no online editor for this one, but there is a command line REPL. PogoScript is DSL friendly allowing white space in function names.

Esoteric Languages

Esoteric languages tend to be write-only, a bit like Perl but just for fun.

Try Brainfuck

Brainfuck is the Rubik’s cube of programming languages. I built the site last year with the interpreter written in plain old JavaScript, check out the fib sample.

Browser IDEs

With so many programming language experimentation environments available online, the next logical step is to host the IDE there. Imagine not having to wait 4 hours for Visual Studio to install.

Cloud 9 is an online environment for creating Node.js apps, pulling together sets of relevant packages. Tools like Sploder let you build games online.

The Try F# site offers arguably the most extensive online learning features of any language. Cloud Tsunami IDE also offers a rich online development experience for F#. In the near future CloudSharper will offer an online IDE experience for developing web applications with F# using WebSharper,

Scaling up

Once you’ve completed some basic tasks in a new language you’ll want to move on to slightly larger tasks. I like to use exercises from the coding Kata Catalogue like FizzBuzz, the Game of Life and Minesweeper.

Some people enjoy going through the Project Euler problems, others have their own hello world applications. For Martin Trojer it’s a Scheme interpreter and Luke Hoban often writes a Ray Tracer.

I’d also recommend joining a local meetup group. The London Scala meetup have a coding dojo every month and the F#unctional Londoners meetup have hands on session in the middle the month, the next one is on Machine Learning.

Programming language books that include questions at the end of sections are a good way to practice what you’ve learned but are few and far between. The recent Functional Programming with F# book is an excellent example of what can be done with questions at the end of each chapter.

While the basics of a language can be picked up in a few hours, expect it to take a few weeks before you’re productive and at least a few months before you start to gain mastery.

Want to write your own language? Pete Sestoft’s Programming Language Concepts book offers a good introduction to the subject.

The Kids Are Alright

In the last week or so I've seen a popular article and a presentation which didn’t paint the rosiest of pictures for the next generation of coders:

I have to agree with Marc Scott that the Information & Communication Technology (ICT) GCSE widely studied in the UK, with Microsoft Office as a focus, seems a bit of a step backwards. The Computer Studies O-level I remember from the mid-80s put coding front and centre. I still have fond memories of my end of year project which consisted of a sprite designer written in Z80 assembler and extensions to the system’s basic language for games programming.

computermuseum

Kids Code

But not everyone at school in the 1980s did Computer Studies or was writing games in their bedroom. What are kids doing in the 21st century? My eldest son Thomas has a range of interests including playing the piano, chess, swimming, scouts, playing video games and coding. His common-or-garden state school has a computer club where they practice Python programming, but unfortunately it is so oversubscribed he couldn’t get a place. He does however write code at home for fun, and believe it or not most of what he has learnt has been off his own back. He started out making his own 3D levels in the Roblox game when he was about 9, and soon progressed to adding his own scripts written in Lua. To give him a little encouragement I bought him a book, Roblox Lua programming written by a teenager. More recently he's been playing with Python on the Raspberry Pi.

Now not all the kids at my son's school code, but quite a few of them do.

Kids Conferences

Thomas has also come along with me to a few conferences and game jams. Last year he came to the Progressive F# Tutorials in London, learning the syntax of the language from Chris Marinos’s F# Koans. Then he managed to make some tunes in Rob Pickering’s Undertone session. Here he is putting a question to the panel:

thomas

Thomas is not alone in knowing F#, here is a 1-hour crash course taught to Year 6 students (11 year olds) at a school in Cambridge: http://fssnip.net/jc

He’s also been to some game jams with me, where he’s mostly worked on the music and sound effects. I’ve seen quite a few kids at this type of event. Here’s a picture of us from last weekend’s GameCraft jam at Skills Matter in London:

GameCraft

Last weekend also saw a national Festival of Code in the UK from Young Rewired State:

yrs-crowd

This fall sees a GOTO conference for kids in Denmark:


I think it would be great to see the trend for kids in conferences continue, and it fills me with hope for the future of programming.

Dark Matter

Brett Victor’s talk on the Future of Programming is well worth a watch, he presents a vision of the future based on what was known in 1973, using an over head projector (OHP). One suggestion is that if programming is still dominated by low-level programming languages in 40 years time (which it is) then something has gone seriously wrong.

dark-matter-doubts_1

After talking about kids going to major developer events, the shame is that the vast majority of existing enterprise developers will have never been to one. These are the unseen 99% Scott Hanselman refers to as Dark Matter Developers:

They don't read a lot of blogs, they never write blogs, they don't go to user groups, they don't tweet or facebook, and you don't often see them at large conferences

I suspect these “Dark Matter Developers” are being spoon fed their information directly from vendors like Microsoft (MSDN) and Oracle (Java).

Speaking at conferences can often feel like you’re preaching to the converted. Perhaps the real opportunity is mentoring the next generation, because you know the kids are alright.

Generative Art

Last night I ran a free hands on Generative Art session to a full class room at Skills Matter for the F#unctional Londoners meetup group. We host a hands on programming sessions every month, next month we’ll return to the Machine Learning theme with Matt Moloney from the Tsunami IDE team.


Samples: http://trelford.com/GenerativeArt.zip

I recently picked up Matt Pearson’s Generative Art book published by Manning, his examples use the Processing programming language which is loosely based on Java. For the hands on creative part we used F# and the SmallSharp library which has a similar feel but is limited to 2D.

SmallSharp

SmallSharp is a small .Net library for drawing graphics, similar to Small Basic but aimed more at the "Sharp" languages C# and F#

Small Basic the good parts:

  • minimal IDE: you get intellisense, buttons for opening and saving files and a big run button (F5)
  • simple library: type GraphicsWindow and dot to start drawing shapes, no need worry about Single Threaded Apartments, data binding or XAML
SmallBasic

Small Basic’s library is just about usable from C# and F# but relies on strings and implicit conversions to a variant type, where as SmallSharp’s API takes explicit typed arguments.

In F# with SmallSharp we can write:

GraphicsWindow.BrushColor <- red
for i in 0..5..200 do
    GraphicsWindow.DrawLine(i,0,200-i,200)
    GraphicsWindow.DrawLine(0,i,200,200-i)

 

Which draws concentric lines:

Pattern

Bubbles

I found a nice piece on Deviant Art entitled Bubbles:

colorful_bubbles_by_basil4life-d6i06vv

The task was to generate a similar work, starting with the following code:

Win.Background <- black
let rand = System.Random()
let colors = [red; green; blue; yellow]
for i = 1 to 200 do
    Win.Opacity <- rand.NextDouble() ** 3.0
    Win.FillColor <- colors.[rand.Next(colors.Length)]
    let x = rand.NextDouble() * Win.Width
    let y = rand.NextDouble() * Win.Height
    let r = 10.0 + rand.NextDouble() * 30.0
    Win.DrawEllipse(x-r,y-r,r*2.0,r*2.0)

Here’s a monochrome from David Kowalski:

Monochrome

and an interesting Spiral effect from Rob Lyndon:

Spiral Galaxy

FunScript

Following Atwood's Law:

any application that can be written in JavaScript, will eventually be written in JavaScript.

I created the same effect using the HTML5 Canvas, with the F# code being compiled to JavaScript by the FunScript library, which also gives typed access to JavaScript libraries.

[<ReflectedDefinition>]
module Program

open FunScript
open FunScript.TypeScript

type ts = Api<"../Typings/lib.d.ts">

let circle (ctx:ts.CanvasRenderingContext2D) (x,y,d,c) =
   let pi = ts.Math.PI
   ctx.beginPath()
   ctx.arc(x, y, d, 0.0, pi * 2.0)
   ctx.fillStyle <- c
   ctx.fill()   

let inline str x = x.ToString()
let rgba (r,g,b) a = "rgba("+str r+","+str g+","+str b+","+str a+")";
let next n = ts.Math.random() * n
let from n = ts.Math.floor(next (float n)) |> int

let main() =
   let canvas = unbox<ts.HTMLCanvasElement>(ts.document.getElementById("canvas"))
   canvas.width <- 1000.
   canvas.height <- 500.
   let ctx = canvas.getContext("2d")
   // Set background
   ctx.fillStyle <- "rgb(0,0,0)"
   ctx.fillRect (0., 0., canvas.width, canvas.height);
   /// Circle colors
   let colors = [
      255,0,0
      0,255,0
      0,0,255
      255,255,0
      ]
   // Draw circles
   for i = 1 to 200 do
      let x = next canvas.width
      let y = next canvas.height
      let r = 10. + next 40.
      let a = next 1.
      let c = rgba (colors.[from colors.Length]) a
      circle ctx (x, y, r, c)

Circles


Turing Drawings

We finished up on drawing roulette with Turing drawings, made by random Turing machines:

DrawingRoulette

I created an F# version a few weeks back which you can run in the Cloud Tsunami IDE.

Have fun!