First steps with purescript

For a while now, I have played around with languages more on the functional side of things. From elixir I came to elm and then had some looks at Haskell (I didn’t write a single line of code in it til now). Lately, in December, I listened to the javascript jabber podcast, Episode 189 , which was about purescript. And it immediately appealed to me.

As discussed in the show, Purescript is a subset of Haskell with some little changes in the runtime semantics (for example strict evaluation in contrast to lazy evaluation), and some other little differences in language constructs (for a complete List have a look at https://github.com/purescript/purescript/wiki/Differences-from-Haskell).

There are other ports of Haskell to javascript like Fay for example. But what makes purescript different and what I like about it is that it was designed to be easily embeddable in javascript and that there is a direct correlation from purescript to javascript code. For example the purescript function:

diagonal :: Number -> Number -> Number
diagonal w h = sqrt (w * w + h * h)

gets compiled into the following javascript function:

var diagonal = function (w) {
return function (h) {
return $$Math.sqrt(w * w + h * h);
};
};

Not only is the generated javascript easy to read, but it’s also properly defined by the language, what makes it a breeze to call purescript functions from javascript. And the FFI is simple to use and allows the integration of existing javascript functions into purescript code. But without going into too much detail let me tell you how my first touch with the language was.

Getting started

I like to try out new things in a fresh vagrant environment. So I set up a new vagrant file with a base presice32 image. I tried to follow the installation instructions for npm. My first try looked something like this:

sudo apt-get update
sudo apt-get install -y curl
sudo curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install — yes nodejs
npm install -g purescript

But the installation exit with some errors. Luckily, there was somebody on github, who had the same problem before (apparently the language designer himself). I basically forked the repo, did a vagrant up and an hour later, I had a working environment for playing around with purescript.

For the first steps, I more or less followed the introductory instructions from the free purescript book. It was easy to follow the examples and pulp makes it very easy to compile and run purescript code. The first example I compiled was the classic “Hello World”.

module Main where

import Control.Monad.Eff.Console

main = log "Hello, World!"

You run that code with pulp run. Pulp browserify generates javascript code intended to be embedded in an Html file. Pulp build -O removes dead code from the generated output. It also ships with an interactive repl which is started with pulp psci. I went on with the examples in the book which includes computing diagonals and printing it to console. To do so, additional packages had to be installed with pulp dep install. There is no additional package manager for purescript. Instead, it uses the existing bower package manager.

Conclusion

Purescript is a nice language and you should give it a try. It is still very young and has some hurdles here and there but I expect these to get away when it gets more mature. I will definitely keep an eye on it and put it to my set of tools.