[Haskell-cafe] Is this haskelly enough?

Michael Vanier mvanier at cs.caltech.edu
Tue Jul 17 19:54:35 EDT 2007


Incidentally, this thread demonstrates a curious feature of Haskell programming.  You write a 
function which works, but somehow you're not satisfied with it.  You stare at it for a while, 
refactor it into a much smaller version, stare at it some more, refactor it again, and on and on 
until your original function is reduced to one line.  Haskell must be the only language which is too 
good at refactoring -- I think I spend as much time refactoring my Haskell code as I do writing the 
original (working) version.  Maybe I'll get better at this as I get more experience (i.e. by 
bypassing the first few stages).

Mike


Dan Weston wrote:
> Nicest. I think your definition has reached nirvana.
> 
> I think a good haskell-cafe thread is like a Shakespeare play. People at 
> every level of experience can get something from it. The early replies 
> answer the question, with follow-on ones exploring the roads less 
> traveled. I for one did not know how to construct the fully pointless 
> version below, and if I hadn't asked, I doubt I ever would.
> 
> I also learned of the list monad this exact same way, so I think its a 
> good and gentle way to introduce people to it.
> 
> Dan
> 
> Bjorn Bringert wrote:
>>
>> On Jul 18, 2007, at 1:00 , Dan Weston wrote:
>>
>>> Bjorn Bringert wrote:
>>>> import Data.List
>>>> maxsubarrays xs = maximumBy (compare `on` sum)
>>>>   [zs | ys <- inits xs, zs <- tails ys]
>>>
>>> I love this solution: simple, understandable, elegant.
>>>
>>> As a nit, I might take out the ys and zs names, which obscure the 
>>> fact that there is a hidden symmetry in the problem:
>>>
>>> maxsubarrays xs = pickBest  (return xs >>= inits >>= tails)
>>>  where pickBest = maximumBy (compare `on` sum)
>>>   -- NOTE: Since pickBest is invariant under permutation of its arg,
>>>   --       the order of inits and tails above may be reversed.
>>>
>>> Dan Weston
>>
>> Nice. Here's a pointless version:
>>
>> maxsubarrays = maximumBy (compare `on` sum) . (>>= tails) . inits
>>
>> Though I avoided using the list monad in the first solution, since I 
>> thought it would make the code less understandable for a beginner.
>>
>> /Björn
>>
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list