[Haskell-cafe] Suggestions for improvement
Dominique Devriese
dominique.devriese at cs.kuleuven.be
Sun Oct 3 16:45:30 EDT 2010
> One question I have is whether I can eliminate points in the above
> definition of blowup, and write something like
>
> blowup = (++) . (blowup . allButLast, lastToTheLength)
>
> thinking of (++) as a function String x String -> String.
Actually (++) is of type String -> String -> String. When you want
something of the type you mean (you normally write that as (String,
String) -> String in Haskell, then you can use (uncurry (++)).
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
Ignore this if you haven't read about Applicative or type classes yet,
but using the Applicative instance for arrow types (->) a, you can
also write
comma = liftA2 (,)
or
blowup = (uncurry (++)) . liftA2 (,) (blowup . allButLast) lastToTheLength
> Also, I can't
> figure out whether it is possible to get a shorter solution using fold.
> I have tried Hlint on my file, but it gave no suggestions.
>
> I am sure there are better ways, and would like some pointers and any
> general suggestions for improvement.
By the way, shorter is not always better. Trying to recognize
abstraction patterns in your code is never a bad thing though.
Dominique
More information about the Haskell-Cafe
mailing list