Phillip Trelford's Array

POKE 36879,255

Random Art

Earlier in the year I came across a Stanford coding assignment inspired by Andrej Bauer’s Random Art. Pictures are built using randomly chosen mathematical expressions that take an x and y value and return a colour. The implementation on Andrej’s site uses OCaml, but is closed source, however a clear Python example is given.

F# version

The Python version worked out-of-the-box but took a while to render (10s of seconds) so I rewrote it in F# (a language based on OCaml) to improve generation time (to less than a second).

The random pictures are unsurprisingly a bit hit and miss so I set up a script to generate batches of 1000 and then sifted through to find ones I liked, here’s a few examples:

Random0093 tartan
Random0937 Random - Copy (9)

 

The image generation is a heavy compute task, running on my i7 Desktop was noticeably faster than my MBP. For yet faster generation the F# code was trivial to parallelise using the Array.Parallel module.

You can run the F# version on Linux, Mac or Windows, just run this snippet as an F# script: http://fssnip.net/si

 

Go version

I’ve been picking up the Go programming language recently, just out of curiosity, and thought this would be an interesting task to try.

Initially I’d used Notepad and the command line on Windows, for this task I switched to Mac and the Atom editor with go-plus, which gives syntax colouring and some code completion and appears to be a relatively popular choice nowadays:

Go Editors

Aside: for editing F# in Atom try the excellent atom-fsharp

Rather than building a UI, I simply used Go’s image package which supports setting pixels and saving images.

Here’s a skeleton using a fixed function:

To produce random art I used an interface to define expressions with each type defined using a structure and associated evaluation function. The expressions are then randomly selected and then composed. The source is available as a gist.

Here's some pictures from the first run:

output472 output518
output164 output670

 

Comparing implementations

The F# and Go implementations feel quite close. In F# I used a discriminated union to define the expression types and functions for evaluation. Similarly in Go I used structures to define the expression data and functions for evaluation. In both cases separating the concerns of data representation and evaluation. In effect, if we ignore the curly braces, programming in Go feels to me more akin to programming in F# than programming C# or Java. Other similarities include multiple return values in Go and first class tuples in F#, and Go’s defer and F#’s use keyword for simple resource management.

 

Hands On Random Art Class

If you’re interested in producing your own random art, I’ll be running a free class at the F#unctional Londoners in October, where we’ll explore some different formulas.

Unboxing FP

How hard is it to get started in functional programming?

Let’s have a look at how quickly you can get started on a selection of simple expression-oriented programming languages.

Today let’s try Clojure, Elm, F#, Haskell and OCaml.

Online REPL

No install required just point your browser at a URL and you’re off:

Language Online REPL
Clojure http://tryclj.com/
F# http://www.tryfsharp.org/
Elm http://elm-lang.org/try
Haskell http://tryhaskell.org/
OCaml http://try.ocamlpro.com/

 

Each language has an easy to use online REPL with simple lessons to get you through the basics. Elm’s online offering lets you edit multi-line programs, as does Try F#, which also includes intellisense in the online editor.

Development environment

Now you’ve covered the basics you probably want to install a lightweight development environment and start building larger programs:

Clojure

I found LightTable very quick to install and setup. The editor comes with psychedelic colours to help you track opening and closing parenthesis:

FizzBuzz Clojure


I’ve been using Stuart Holloway’s Programming Clojure book as a guide.

Elm

Elm has a very usable online editor, a simple installable REPL, and a wonderful playground feature with Elm Reactor:


F#

If you’re on Windows and have Visual Studio installed then you’ve already got F#. From the file menu click New and select a new F# project or script file.

No Visual Studio, no problem, for Windows the Tsunami IDE is a fast 25MB download, which gives you the latest compiler and an editor with intellisense:

Tsunami IDE

On Mac I’d recommend Xamarin Studio and for Linux MonoDevelop or Emacs.

Functional Programming using F# and Dave Fancher’s recent Book of F# are both great introductory texts.

Haskell

The Haskell platform gives you a compiler and REPL from a simple 100MB install. A combination of a text editor along with the REPL gets you going in no time:

Haskell FizzBuzz

As a guide I’ve been using the Real World Haskell book. Learn you an Erlang some Haskell for great good! looks like a fun read too.

OCaml

Like Haskell, OCaml is bundled in a simple installer and includes the compiler and REPL. Choose your own editor and use the REPL to explore your programs.

I recently picked up OCaml from the Very Beginning and More OCaml, which are both nice concise introductions.

OCaml From The Very BeginningMore OCaml

Conclusions

Using an online REPL you can get started with any of these languages in seconds, and there are plenty of lightweight install options too. Combine that with a good selection of learning resources from books to online courses, and we can conclude that nowadays it’s really not that hard to get started with FP.