[Haskell-beginners] Another request for code critique...
Brent Yorgey
byorgey at seas.upenn.edu
Fri Apr 1 17:07:12 CEST 2011
On Thu, Mar 31, 2011 at 02:18:32PM -0400, Mike Meyer wrote:
> Shorter, but thing I've done that wasn't just an exercise from a
> haskell book.
>
> The problem statement can be found at
> http://kernelbob.wordpress.com/2011/03/20/same-five-digits/.
>
> My solution can be seen at http://pastebin.com/iW95q2ex.
Looks nice. A few places things could be made a bit more points-free
(which isn't always a good thing -- but I think in these cases it makes
things more readable, although I suppose that's mostly an issue of
what you're used to)
(\ s -> length s < 6) ---> ((<6) . length) (etc.)
(\ (_, m) -> (== 5) $ M.size m) ---> (==5) . M.size . snd (etc.)
If you have a recent enough version of base, (\ (t,m) -> (m,t)) is
available as 'swap' in Data.Tuple.
This one is slightly more advanced:
map (\ (t,m) -> (t, head . M.keys $ M.filter (== '1') m))
can be replaced by
(map . second) (head . M.keys . M.filter (== '1'))
which applies the function (head . M.keys . M.filter (== '1')) to the
second component of every element of a list. The 'second' function is
from Control.Arrow, and can be given the type
second :: (b -> c) -> (a,b) -> (a,c)
(actually its type is a bit more general than that).
-Brent
More information about the Beginners
mailing list