[Haskell-beginners] general observation about programming
Michael Orlitzky
michael at orlitzky.com
Fri Feb 26 22:08:06 UTC 2016
On 02/26/2016 12:41 PM, Rein Henrichs wrote:
> Pointfree is good for reasoning about *composition*. It can often be
> more readable than pointful code when the focus of the function is on
> composition of other functions. For example, take this function from
> Bird's /Pearls of Functional Algorithm Design/:
>
> boxes = map ungroup . ungroup . map cols . group . map group
>
> Compare the pointful version:
>
> boxes matrix = map ungroup (ungroup (map cols (group (map group matrix))))
>
> Readibility is subjective, but I think many people will agree that the
> pointfree version is easier to read.
>
Sure, but does anyone have any idea what that first version is supposed
to do? It would be much better to write it out:
boxes matrix = one_big_list
where
-- The function (group . map group) produces a list of box
-- matrices, transposing each one in the process.
transposed_box_matrices = group (map group matrix)
-- The "cols" function performs a transpose, so mapping it over
-- transposed_matrices gives us the box matrices we want.
box_matrices = map cols transposed_box_matrices
-- Now we concat everything into one big matrix for some reason.
-- This is the inverse of the (group . map group) that we did
-- earlier.
one_big_list = map ungroup (ungroup box_matrices)
You don't even need to write a book to explain what it does =)
More information about the Beginners
mailing list