Set permutations

I like array languages like APL and J. I don't actually create anything useful with them, but the alien syntax of the former and the tacit nature of the latter have interested me for a few years now. I write some Haskell in my day job, which is also point-free, and have leveraged J's notions of hooks and forks to write cute, clean, and composable code. I don't think J itself looks nearly as nice, which is one of the reasons why I never use it.

I familiarized myself with these control constructs by writing a point-free J function that returns all permutations of a set. Take these three items as an example.

   X =: 'dog' ; 'cat' ; 'magpie'


Then the following blurb will give its permutations.

   (#~#:@i.@(2^#)) X
│      │      │      │
│magpie│      │      │
│cat   │      │      │
│cat   │magpie│      │
│dog   │      │      │
│dog   │magpie│      │
│dog   │cat   │      │
│dog   │cat   │magpie│

It's a stupid trick that relies on the way integers are represented in binary.

  1. Let n equal the number of items in the set.
  2. Generate binary representations of 0 ... 2n-1.
  3. Use these as truth vectors (1 = show item, 0 = don't) mapped across the set.

J's handling of matrices and integer truthiness facilitates this nicely. Some hooks, composition, and reversal of arguments ensure that I never have to specify a variable in the function body. I suspect that my procedure can be terser still, considering my level of experience.

But is it elegant? I don't think so. Being a good tacit programmer most likely requires a ruthless editorial approach; if something hampers readability, then it needs to be axed regardless of how cool it might be conceptually.