Phillip Trelford's Array

POKE 36879,255

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.

Oops

Are you working in the enterprise?

Do you find yourself, day-in-day-out, up to the eyeballs in unmaintainable code?

Does the once beautiful architecture now more closely resemble a big ball of mud, that no amount of tooling will dig you out of?

What can you do?

1) Bury your head in the sand

head-in-the-sand

A very popular option, you just need to keep practising denial.

2) Turn to drink

gazza

Another popular option, although unfortunately this strategy is only likely to last as long as your liver.

3) Become a scrum master

scrum master

This is an easy way out, scrum certification is just a 2 day course away, but there’s probably no looking back.

4) Admit there’s a problem

AA

This is one of the hardest and least popular options, but possibly the most rewarding.

Start by saying out loud: “Object Oriented Programming is an expensive disaster which must end” and then take each new day as it comes.

C# 6 Cuts

In a recent thread on CodePlex Mads Torgeson, C# Language PM at Microsoft, announced 2 of the key features planned for C# 6 release have now been cut:

  • Primary constructors
  • Declaration expressions

According to Mads:

They are both characterized by having large amounts of downstream work still remaining.

primary constructors could grow up to become a full-blown record feature

Reading between the lines Mads seems to be saying the features weren’t finished and even if they were they seemed to conflict with a potential record feature currently being prototyped.

The full thread is here: Changes to the language feature set

Language Design

I think there’s two distinct options when adding new features to an existing language with a large user base:

  • upfront design
  • implement incrementally

Upfront design should mean that all cases are met but comes at a time-to-market cost, where as an incremental implementation means quick releases with the potential risk of either sub-optimal syntax or backward compatibility issues when applying more features.

It appeared at the high level that the C# team’s had initially opted for the incremental option. The feature cuts however suggest to me that there may have been a change in direction towards more upfront design.

Primary Constructors

The primary constructors feature was intended to reduce the verbosity of C#’s class declaration syntax. The new feature appeared to be inspired by F# ’s class syntax.

If you like the idea of a lighter syntax for class declarations then you may just want to try F# which already has a well thought out mature implementation, i.e.

type Person(name:string, age:int) =
    member this.Name = name
    member this.Age = age

Or for simple types use the even simpler record type:

type Person = { Name:string, Age:int }

Note: on top of lighter class syntax F# also packs a whole raft of cool features not available in C#, including powerful pattern matching and data access via Type Providers.

Declaration Expressions

Declaration expressions was again designed to reduce verbosity in C# providing a lighter syntax for handling out parameters. Out parameters are used in C# to allow a method to return multiple values:

int result;
bool success = Int32.TryParse("123", out result);

Again handling multiple return values is handled elegantly in F# which employs first-class tuples, i.e.

let success, value = Int32.TryParse("123")

As shown above, C# out parameters can be simply captured in F# as if the method were returning multiple values as a tuple.

Conclusion

The first time I saw Mads publicly announce the now cut primary constructor syntax and declaration expressions was nearly a year ago at NDC London. At the time the features were announced with a number of disclaimers that they may not actually ship. I think in future it may be better for everyone to take those disclaimers with more than just a pinch of salt.