[Haskell-cafe] Suggestions for improvement

Victor Nazarov asviraspossible at gmail.com
Sun Oct 3 17:52:05 EDT 2010


I suggest to pay more attention to haskell's standard library.

"allButLast" is called "init" in Data.List module.

Second, do not use explicit recursion. You can capture recursion using
some high-order function like map, filter, foldr and so on:

lastToTheLength xs = map f xs
  where f = const . last $ xs

And last, your type signatures are too restrictive. You can apply your
functions to arbitrary lists.

lastToTheLength :: [a] -> [a]

Standard library knowledge is very helpful in producing short and
clear definitions.

blowup = concat . zipWith replicate [1..]

On Mon, Oct 4, 2010 at 1:24 AM, Dominique Devriese
<dominique.devriese at cs.kuleuven.be> wrote:
> Gregory,
>
> 2010/10/3 Gregory Crosswhite <gcross at phys.washington.edu>:
>>  On 10/3/10 1:45 PM, Dominique Devriese wrote:
>>>
>>> Additionally, you can't combine the functions (blowup . allButLast)
>>> and lastToTheLength into a function that returns a pair like you seem
>>> to attempt. You need a function like the following for that:
>>>
>>> comma :: (a ->  b) ->  (a ->  c) ->  a ->  (b,c)
>>> comma f g x = (f x, g x)
>>>
>>> Then you could say:
>>>
>>> blowup = (uncurry (++)) . comma (blowup . allButLast) lastToTheLength
>>
>> It is worth noting that such a function already exists in the standard
>> libraries;  it is the &&& operator in Control.Arrow:
>>
>>    blowup = uncurry (++) . (blowup . allButLast &&& lastToTheLength)
>
> Or you can write it as (liftA2 (,)) as I noted a few lines further in my mail ;)
>
> Dominique
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
Victor Nazarov


More information about the Haskell-Cafe mailing list