[Haskell-cafe] Re: Bathroom reading
westondan at imageworks.com
Thu Aug 16 21:51:15 EDT 2007
I hate to be a party pooper, but isn't this just:
> f = foldr (\a z -> (a:snd z,fst z)) (,)
This takes less time to grok and takes no longer to run.
But why stop there?
> import Control.Arrow
> swap = snd &&& fst
> f = foldr (\a -> swap >>> first (a:)) (,)
Just read this aloud to see what it does (without the tilde encryption
algorithm at work!).
More importantly, there is manifestly no pattern failure to check for here.
It is fun that with ~p we don't need deconstructors for p (reminds me of
Lisp's setf), but how many types have exported constructors with no
And Arrow notation makes up for the loss of fun, so you don't feel
P.S. Either doesn't count, because you can easily roll your own
getLeft = either id undefined
getRight = either undefined id
but I honestly don't know why these aren't in Data.Either.
Or for that matter, why swap is not in Control.Arrow, since it comes in
handy quite frequently with pointless programming.
> Someone mentioned the "Blow your mind" page.
> One example there really caught my attention.
> "1234567" => ("1357","246")
> foldr (\a ~(x,y) -> (a:y,x)) (,)
> I've known about lazy match since an early version of the Haskell
> report, but have never actually used it. Last night, looking at
> that example, the lights went on and I finally grokked why it's
> there and understood when/why I might use it myself.
> Oh, I knew perfectly well what it does. It just never made itself
> at home in my head.
> I'd like to recommend this example for some sort of prize, therefore.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe