read


read
or learn more

pesto5: A concatenative programming library in 5 lines of Clojure

Jan 6, 2013

Concatenative styles are making the rounds lately including some interesting examples in Clojure and Haskell. Now I’m perpetually drawn to concatenative languages and their ideas, so I wanted to know what it would take to provide a minimally representative concatenative experience in Clojure.1 As it turns out… not much:

(defn postfix [& e]
  (reduce #(if (fn? %2)
             (let [[l r & m] %]
               (cons (%2 r l) m))
             (cons %2 %)) [] e))

This is how it’s used:

(postfix 5 1 2 + 4 * + 3 -)
;;=> [14]

The way that it works is that it treats every non-function as a value and every function as a binary function taking two elements off of a “stack”. It then wraps the answers back up onto the “top” of the stack. The final answer is the stack itself.

This is in no way indicative of a fully blown representation of concatenative programming languages,2 but it was fun and it highlights the stark simplicity of the model. This is my year to finally take concatenative languages seriously. I plan to study hard. I hope you’ll join me.

:F

if you want to talk concatenative languages or pesto5, then feel free to comment below or email me at the address at the top of my blog


  1. pesto5 is too small to be a “library”, but a gist will do

  2. It’s debatable if this is concatenative at all, but alas the name pesto5 was too alluring to pass on. 

7 Comments, Comment or Ping

  1. Not sure any discriminating hacker could participate until you at least mention Forth :P lol.

  2. Great, simple example. Nice to see people interested in concatenative languages.

    If you’re interested in a different take on concatenative programming, check out Om. It’s a brand new, experimental concatenative language that uses prefix notation: instead of a data stack, each function takes the remainder of the program for rewriting.

    At concatenative.org: http://concatenative.org/wiki/view/Om Website: http://om-language.org

  3. try forth familly languages

  4. You might have a look at Consize, a concatenative language I implemented in Clojure in 140 LOC. Consize is very much inspired by Factor, however Consize is a functional language like Joy or Cat is.

    http://code.google.com/p/consize/

    It is extensively documented in German — I plan to add some documentation in English as well. But you as an Clojure Expert might enjoy reading the source code anyhow ;-) After reading consize.clj, I recommend to continue with prelude-plain.txt.

    BTW: Adding object-orientation (i.e. polymorphism via generic words & multiple inheritance much like Clojure does) is possible in about 30 LOC.

    Dominikus

  5. I prefer to speak of stack programming :). But it was one of my first prefered paradigm before functionnal programming.

    There was a goog article by Jon Purdy : http://evincarofautumn.blogspot.ch/2012/02/why-concatenative-programming-matters.html.

    and Brandon Bloom the author of the recent fipp wrote a clojure modure based on Factor, a quite interesting approach. https://github.com/brandonbloom/factjor

    I thought about stack languages as cuda interface.

  6. David Liebs and I threw together a concatenative mini-language in Mathematica that does mixed symbolic and numeric computation. The entire thing is implemented, with tooling, in just a few conditional rewrite rules. That’s what is really lovely about concatenative techniques: you get a LOT of expressive power in a very SMALL amount of code, i.e., signal-to-noise ratio is huge!

  7. wordpress blog designers

    I always enjoyed visiting this specific website once weekly or possibly even longer since you really do usually have got several very good posts so congratulations coming from one of the fans!

Reply to “pesto5: A concatenative programming library in 5 lines of Clojure”