In Clojure We Trust

A piece of intertube about the Clojure programming language, algorithms and Emacs.

Laser Cutting With Love

In this post I will show you how I conceived a beautifull laser-cutted wooden cube.

cube_dice.jpg

Figure 1: Two of the cube faces

The first step I took was to conceive a 3D model of the cube to ensure that each piece fit well when assembled. I wanted to used open-source tools so I used the FreeCAD software, which was unfortunately buggy. Undo/redo was not working properly (tip for the developpers of FreeCAD: use immutable data-structures!) so I used Git for snapshotting the model in case anything went wrong. Each face of the cube was then projected on a plan inside FreeCAD. You can find the final work here. There are already existing tools and websites to conceived such wooden cubes or boxes with laser-cutters but I wanted to do everything myself.

The second step was to edit the files in Inkscape to add the letters and specify the cutting, engraving and marking areas with different colors.

The third step was to import the file in VisiCut and map the color to the laser operations and adjust the settings. Different laser cutters models have different software requirements.

The fourth step was to calibrate the laser. This is not as hard as it sounds. Basically you just need to adjust the distance between the laser head and the wood. It is easy to do after somebody explain it to you.

The fifth step was to do a few experiments on the laser cutter to get the setting right. For example the first trial did not cut through the wood, then the intensity of the marking was not good enough etc. Playing with the speed and the power of the laser allows to get different results.

How does a laser-cutter looks like? Look on YouTube.

This is the cube once cutted and assembled (without glue):

cube_full.jpg

Figure 2: Final result

It makes a really nice present:

cube_wrapped.jpg

Figure 3: A gift

You can get access to laser cutters at your local FabLab / Maker-Space / Hacker-Space.

Lambda Calculus, a Suprisingly Powerful Language

I’m reading the book Types and Programming Languages from Benjamin C. Pierce from time to time at the moment. The chapter explaining the untyped lambda calculus gave me more than one occasion to pause and think. The lambda calculus, invented in the 30s, is a formal system for expressing computation with functions and can also been seen as a programming language. A very simple one, yet a surprisingly very powerful one. A language where all computations are done with functions, thus the first functional programming language in the history of mankind. It plays an important role in the field of research but also as a theoretical foundation for implementing functional languages such as Haskell, OCaml, Scheme, Clojure etc.

The lambda calculus is composed of three elements (or in programming language theory jargon, of three terms):

  • variables, of the form x
  • abstractions, of the form λx.t
  • applications, of the form t s

An abstraction can almost be seen like a function in “classic” programming languages. An application “applies” its arguments to an abstraction. There is nothing more. There are no booleans, numbers, strings, conditional statements etc. Functions accept only one argument. Yet, everything can be computed with the lambda calculus.

A variable x is bound if it occurs in the abstraction of the body t of an abstraction λx.t. It is is free if it appears if it is not bound by an enclosing abstraction on x. For example in (λx.x) x the first x is bound, the second one is free. A term with no free variables is called a combinator. 1

The simplest combinator is the identity function. It returns its argument unchanged:

id = λx. x

There are different strategies when choosing on how arguments are evaluated before applying them to functions. That is, there are different strategies to reduce the terms. The most common one is call-by-value. Under this strategy a function can be applied only when its right-hand side has been reduce to a value. A value is a term that cannot be further reduced.

Thus (λx.x x) (λx.x) y reduces to (λx.x) (λx.x) y in the first evaluation step then to (λx.x) y then to y. Functions with multiple arguments can be simulated by having a function returning another function: λx.λy.x y, for example. This is familiar to functional programmers as currying.

What really impress me is that every computation are possible with such a simple language. I will give a few examples from the book, some are answers from exercises, so I you indent to read it I would suggest to stop your reading here.

Boolean values can be encoded as functions like this:

tru = λt. λf. t

fls = λt. λf. f

Tru is a function that discards the value of its second argument and returns the value of its first argument. Fls is a function that discards the value of its first arguments and returns the value of its second argument. They are used as a representation of the True and False booleans.

The test combinator can be defined like this:

test = λl. λm. λn. l m n

If the first argument of test is tru the expression will reduce to m, if it is fls it will reduce to n. For example test tru x y evaluates to tru x y which evaluates to x. Awesome, it is almost like a if statement!

Numbers can be encoded too. The Church numerals 2 are defined like this:

c0 = λs. λz. z

c1 = λs. λz. s z

c2 = λs. λz. s (s z)

and so on and so on. Functions such as scc (successor) and plus can also be defined.

scc = λn. λs. λz. s (n s z)

plus = λm. λn. λs. λz. m s (n s z)

Data structures such as pairs can also be encoded:

pair = λf. λs. λb. b f s

fst = λp. p tru

snd = λp. p fls

Take a paper and a pen and evaluate fst (pair v w). It evaluates to v, as expected.

Lists can also be encoded. The list [x, y, z] can be encoded as the abstraction λc. λn. c x (c y (c z n)). That is a fold (also known as a reduce) is used to encode the list.

The usual functions to manipulate lists can be defined:

nil = λc. λn. n

isnil = λl. l (λh. λt. fls) tru

cons = λh. λt. λc. λn. c h (t c n)

head = λl. l (λh.λt.h) fls

tail = λl. fst (l (λx. λp. pair (snd p) (cons x (snd p))) (pair nil nil))

It is also possible to encode lists with pairs.

A mechanism similar to recursion can be defined by having functions reducing their arguments to a form similar to the function itself. For example this combinator:

(λx. x x) (λx. x x) reduces to himself.

More advanced tricks allow to define the equivalent of recursive functions such as the canonical definition of factorial.

Lambda calculus is a very simple but amazingly powerful language. It is also striking to make the comparison with some programming languages such as Java that could not even pass simply a function around before the most recent versions.

Congratulation for reading until there! Given all the shortcuts I took to explain, if you understood everything, well done.

Footnotes:

1

I’m paraphrasing the book here and also a lot below!

2

Alonzo Church invended the lambda calculus.

Four Haskell Books

In 2013 and 2014 I read four Haskell books. In this post I’m making mini-reviews for each of them, chronologically ordered.

1 Learn you a Haskell for Great Good

Learn you a Haskell for Great Good is one of the best computer science book I had the occasion to read, and I even admit I was a little sad once I finished it! It exposes the main concepts of Haskell in a very pedagogic way and clear writing style. After reading the book, functors, applicative and monads become easily understandable. The only drawback is that this book is not enough to start with Haskell, there are no explanations of how to create a Haskell project using Cabal, use libraries, test your application etc. I think it’s okay for an introductory book but one should be informed. The book is suited for people without experience with Haskell or functional programming.

2 Developing Web Applications with Haskell and Yesod

I read Developing Web Applications with Haskell and Yesod to discover other ways of building web applications, as I mostly had experience with Clojure and Compojure. I enjoyed reading this book which is short but contains a lot, going from the frontend to the backend and explaining a lot of subjects: HTML templates, sessions, authentification, persistence with databases etc. You discover how a statically typed language can help you building a web application with more guarantees. The only drawback for me was that even after reading one book on Haskell, some of the type signatures of functions were still hard to understand. Some familiarity with monad transformers prior to reading this book may help.

3 Real World Haskell

The goal of Real World Haskell was to bring Haskell to a less academic audience by showing how Haskell could be used for “real world” applications, at a time where there weren’t so many Haskell books there. In this sense I think the book succeed. There are a lot of interesting subjects tackled in this book, like profiling and performance analysis but I did not really enjoy reading it. Either the examples were a bit boring or the writing style was too dry for me, in the end I had to fight to finish this very long book (~700 pages!). I nonetheless appreciate that this book exist and I may use it in the future as a reference. The book is a bit outdated and some code are not valid anymore ; it was not a problem for me since I didn’t try out the examples but this should be considered if you want to learn Haskell with it.

4 Beginning Haskell

Beginning Haskell is a paradoxical book. The truth is this book should not have been published because its edition and proof-reading are too bad. What do you think about a book where the first code sample is wrong? map listOfThings action instead of map action listOfThings on page 4, come one… Also the subtitle reads “a Project-Based approach” but this seems to be exaggerated since you will only get some code samples there and there… That being said, I really enjoyed reading this book! I don’t know if it’s a good introductory book but it’s surely is a correct second book on Haskell. It explains how to use Cabal, test an application, use popular libraries like Conduit, build parsers with attoparsec, building a Web Application with Scotty and Fay etc. It includes advanced topics like type programming (enforcing more constraints at the type-level) and even a small introduction to Idris. Conclusion: either burn it or read it.

5 Conclusion

There are more and more books on Haskell newly published or coming, this makes me happy as this broaden the Haskell community, shows that it is growing and makes it easier to learn the language. Reading is not enough to learn so at the moment I’m writing small Haskell projects to go from a beginner to intermediate level.

Playing With Haskell

After reading a few books on Haskell I had to face the reality: learning by doing a project was necessary!

I chose a project which was easy enough to be finished in a few weeks or months (but still slightly challenging) and had a practical utility.

My project is a JSON command-line utility loosely inspired by underscore-cli and Hawk. Arbitrary Lens expressions can be used to filter or transform the input.

If you don’t know what Lens are, think of them as getters/setters/filters/functions combinators similar to JQuery or CSS selectors but for any type of data-structures. I’m still a beginner regarding Lens.

The challenge for me was to learn how to dynamically evaluate Haskell expressions. This is uncommon since Haskell is statically typed. The library I used to do that is naturally limited in its functionality in comparison to a Lisp but the final result is all but disappointing.

For the purpose of my program I implemented a pretty printer for JSON similar to aeson-pretty but with colors. Maybe I should package it for Hackage?

Once I had hdevtools setup for cabal sandboxes, programming in Haskell was enjoyable. Refactoring is easy thanks to the strong type system. I was stuck once or twice with the type system but the people on the #haskell channel were helpful. The code has a certain form of esthetic even if I feel more knowledge would allow me to be cleaner. For example I wonder if it is possible to avoid pattern matching on Left and Right for multiple calls which return something like IO (Either x y), since both IO and Either are monads.

You can have a look at the project here:

https://github.com/kototama/jp

A Simple Emacs Configuration

In this post I introduce a simple, effective and maintainable structure for the .emacs.d configuration directory.

I have a mix feeling about pre-defined Emacs configurations such are Emacs Live or Prelude. In one hand they build a nice showcase, demonstrating powerful modes and features, in the other hand they seem complex in their structure and use their own abstractions (“modules” for Prelude, “pack” for Emacs Live). That is why I still prefer to write my own configuration which I can easily fix. Of course I still dig in people configuration to get ideas. Basically I have reached the same conclusion as technomancy which stopped its Emacs Starter Kit.

In the last weeks I have redone my configuration removing El-Get, which I found too slow, in favor of MELPA. My initial switch to El-Get was because Marmalade was often down ; I didn’t experience the same issues with MELPA.

The configuration is based on package+ (the name is hard to search on the web!) and the great use-package package. A makefile installs the few git submodules needed, download user-defined packages and byte-compiles the packages. User and machines specific configurations can be defined in the $HOME/.eroles/login@hostname/ directory. The auto-async-byte-compile mode ensures that Emacs Lisp files are always byte-compiled.

All packages configurations are done in a lazy way, thanks to use-package, and organized with orgstruct-mode in an org-like structure.

I stole a few ideas here, there and there for the configuration.

The configuration is simple yet effective, I hope it can inspire beginners or even advanced users for how to structure an .emacs.d directory effectively.

See my .emacs.d configuration on GitHub and happy hacking!

An Emacs Minor Mode to Mute Email Threads

Emacs is best know to be a powerful text editor. It is also a powerful note editing system with org-mode. Since emails are mostly composed of text it also makes sense to use Emacs for reading and writing them ; automating common tasks is possible with Emacs Lisp and taking notes and referencing emails within org-mode is easy.

To read my emails with Emacs I use the notmuch program. It indexes emails very quickly and functions with tags. Searching is super fast, even with a huge number of emails.

Since Emacs is almost infinitely customizable, I hacked a minor mode for notmuch that allow me to mute email threads. You can mark a thread of emails as ‘killed’ and all incoming email belonging to this thread will be automatically marked as read upon arrival.

The mode is not very efficient for the moment but you are encouraged to try it on your own and see if it improves your workflow.

You can find the mode on GitHub. Happy hacking!

Linux Distributions as a Service

One of my colleague installed Ubuntu on his Mac computers and was surprised by the varieties of Ubuntu distributions offered. There are different distributions and differents architectures and he was wondering how they managed this complexity.

This gave me an idea. What about having even more distributions? What about having personalized distribution customized for your particular machine ?

A usage scenario could be like this:

  • you go on the website of a Linux distribution,
  • start an application that analyze your hardware,
  • answer a few questions about your requirements (security, network, etc),
  • choose the graphical environment, choose or customize some graphical themes (for a desktop distribution) etc.
  • you then get a link where you could download your own customized distribution, tailored for your needs and hardware. Or maybe you enter an email and you get a link via email when the build is finished.

Common choices and typical hardware configurations could be shared by the community.

It would require a good infrastructure but it could be a service people would be ready to pay for.

Currently only expert Linux users can tailor their distributions for their needs, Linux distributions as a service (or “on demand”) could bring more power and more control to a broader public.

Migrating Away From Google Blogger

This blog is moving away from Google Blogger. Update your RSS reader.

Moving to a self-hosted platform will allow me to:

  • have a better control of my data. My blog can now be versioned on GitHub and do not depend of Google anymore.
  • have a better workflow. I can now blog in plain text with Emacs, Octopress and org-octopress.

Of course the reliability may not be as good as the Google services but in general I think I will enjoy the journey!

Searching in Clojure and ClojureScript Files With Ack

“Ack is a tool like grep, optimized for programmers”. I knew the existence of Ack since a few years but I never gave it try. Now that I did, I’m positively surprised: it’s easy to use and fast.

I was always fighting in Emacs between grep (what is the syntax?), igrep-find (why did it suddenly stopped to prompt for directories?), tags-search (ooops some of my files are not tagged) and rgrep (oh it did exist?). I installed ack-and-a-half for Emacs and its great. Ack-and-half tries to find automatically the root directory of your project but you can set ack-and-a-half-prompt-for-directory to true with (setq ack-and-a-half-prompt-for-directory t) for it to ask which directory to search in.

The Ack version on the website provides support for Clojure files. For ClojureScript files, you need to add the following lines to your ~/.ackrc file:


--type-add
clojure=.cljs

Et voila, enjoy!

Duplicating S-expressions on a Line

Duplicating a line is extremely useful when editing text and specially when programming. There is no native command in Emacs to achieve that but it’s easy to add one.

It’s easy to get addicted to the use of this new command but a problem remains when programming in Lisp with paredit.el: duplicating sometimes lead to invalid s-expressions being inserted.

I decided to give it a try and made an implementation that duplicate the s-expressions after the point (cursor). For instance in Clojure if you are editing this code:

(ns myns
(:use [clojure.string :only [escape]]))
You can duplicate the first vector by placing the cursor on the square bracket, invoking the command with one keystroke and then easily obtain this code:
(ns myns
(:use [clojure.string :only [escape]]
[clojure.set :only [union]]))
Here is the code to duplicate the line; to my knowledge there is no such command in paredit.el:

Not really pretty but it does the work, feel free to provide a nicer implementation in the comments or add it to your ~/.emacs.d/init.el file with:

(eval-after-load "paredit"
'(progn (define-key paredit-mode-map (kbd "C-S-d") 'paredit-duplicate-after-point)))
Edit: Here a fix for when the sexp is the last expression in the buffer.

Comments

Neale Swinnerton
Nice idea. Have you tried expand-region ? It doesn't do quite what you're trying here, but it handles most of the 'sexp that straddles a line ending issues'