Evan Martin (evan) wrote in evan_tech,
Evan Martin

run length encoding and arrows

Run length encoding in Haskell casually tosses in this bit:
encode = map (length &&& head) . group
Which produces RLE'd tuples:
> encode "aaaabbaaa"

But what's this &&& bit? It's from Control.Arrow, the implementation of arrows, which are reportedly a generalization of monads. The last time I tried to look at this was before I grokked monads so it's probably worth another look, but the superficial understanding needed here is that &&& runs two functions "in parallel" (not as in simultaneously, just in that they're not interacting) and produces a pair of their two outputs.

That is,
> :t length &&& head
length &&& head :: [a] -> (Int, a)

where the first component of the tuple is the length and the second is the head of the list.

What a peculiar but also imaginably useful operator! (Of course, in the arrows docs the application of this just to functions is the most basic of the many generalizations.)
Tags: haskell

  • your vcs sucks

    I've been hacking on some Haskell stuff lately that's all managed in darcs and it's reminded me of an observation I made over two years ago now (see…

  • ghc llvm

    I read this thesis on an LLVM backend for GHC, primarily because I was curious to learn more about GHC internals. The thesis serves well as an…

  • found my bug!

    Not too interesting, but this has been bugging me for a week. Been working on a toy program that proxies a TCP connection. It was working fine for…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.