Archives for Clojure

Equational Reasoning

Alan Dipert tweeted an interesting puzzle: pop quiz: solve point-free. answer must be a function value! #clojure The 4clojure version of the problem is simple enough, but the added requirement of a point-free solution makes Alan’s version of the problem harder. And harder to resist. So I took a shot at it. My plan […]

An invitation to FP for Clojure noobs

I’ve heard newcomers to Clojure ask how to get started with functional programming. I believe that learning to program in the functional style is mostly a matter of practice. The newcomer needs to become familiar with a handful of higher order functions, and how they are used in common idioms. This can be done by […]

Tree reparenting

Chris Houser contributed the very pretty problem 130 to 4clojure. I won’t repeat the problem statement here, just my thoughts on how to approach the problem, and my solution. The strategy is to decompose a complex task into a sequence of simpler tasks. The complex task is to transform a tree: we are given a […]

A simple parallel computation

The 2 June 2011 issue of The Economist contains an article on parallel programming. The article points out what is obvious to some, mostly because they have learned it the hard way. You might expect a six-core machine to be six times faster than a machine with a single-core microprocessor. Yet for most tasks it […]

map, mapp, and mapc

In a 1988 paper called Algebraic Identities for Program Calculation, Richard Bird wrote: Probably the most useful law about map is the fact that it distributes over functional composition: (map f) . (map g) = map (f . g) Bird’s paper predates Haskell, and instead uses a language called Miranda, but Haskell later adopted very […]

The transpose function

I am going to discuss a function I think belongs in the Clojure core language. The function is called transpose. It is defined as follows. (defn transpose [xs] (apply map vector xs)) Here is what it does: user> (def m [ [‘a ‘b ‘c ‘d] [1 2 3 4] ]) #’user/m user> m [[a b […]

Cracker Barrel puzzle

A recent posting to the Trifunc group invited the members to solve the Cracker Barrel puzzle. Brian Adkins, the author of the challenge, posted a solution in Haskell. Here is my Clojure solution. The game is played on a triangular board with 15 holes, labeled 0 through 14. Initially, 14 of the holes contain pegs, […]

A thread safe fsm?

Today I wanted to play around with building a small thread-safe finite state machine. I’m mostly interested in the thread safety angle, so the machine is kept extremely simple. Clojure’s protocol feature turned out to be just what the doctor ordered. The fsm consists of three things: the current state, the input alphabet, and a […]

bind, unit, and all that

I’ve given in to the unholy urge to write a monad tutorial. Here are some remarks that might help you decide whether it’s worth your time to read on. My intended readers are programmers interested in functional programming, not mathematicians. No category theory is required. All the code will be in Clojure. The level of […]

The expressiveness of functional programming

I’d like to revisit the discrete convolution problem described in my previous post, to consider the expressiveness of functional programming. Here I am interested in the power of code to communicate intent; the bare metal speed of the code is secondary. The various comments and proposed implementations in this Google Groups thread are what got […]