[Haskell-cafe] Dynamic choice of "reverse" implementation

Krzysztof Kościuszkiewicz k.kosciuszkiewicz at gmail.com
Fri Sep 28 11:59:38 EDT 2007


On Fri, Sep 28, 2007 at 04:38:35PM +0100, Brian Hulley wrote:

> > In my oppinion reversor would have type
> >  
> >> reversor :: (Foldable f) => [a] -> f b
> >>    

> No, this is the wrong type. To find the correct type, if you look at the 
> type of the input argument in your code it will be the result of 
> (lines), so from ghci:
> 
> Prelude> :t lines
> lines :: String -> [String]
> Prelude>
> 
> Therefore (reverseor) has type [String] -> ???
> Now for the output type, you are using (output) as an input to (mapM_ 
> putStrLn). (mapM_) takes a list and uses its argument to do something to 
> each element of the list.

True. I forgot to mention imports in my code:

> import Prelude hiding (foldr, foldr1, reverse, mapM_)
> import System.Environment
> import Data.List hiding (foldr, foldr1)
> import Data.Foldable
> import Data.Traversable
> import Data.Sequence

So the type of mapM_ used in the code is
(Foldable t, Monad m) => (a -> m b) -> t a -> m ()

I'd like to keep the generic Foldable t there when "m" is specialized to IO.
I thought this would allow type of "reversor" to be specialized to
(Foldable f) => [String] -> f String

> For using Data.Sequence to implement reversor, all you need to do is 
> first convert [String] to Seq String, reverse the sequence, then convert 
> back from Seq String to [String].

Yes, probably that's how it works under the hood, but the reason I mentioned
Foldable is that I'd like to avoid [a] -> something -> [a], but keep the
type of output value from "reversor" abstract... For no particular reason,
just playing with this idea :)

Regards,
-- 
Krzysztof Kościuszkiewicz
Skype: dr.vee,  Gadu: 111851,  Jabber: kokr at jabberpl.org
Mobile IRL: +353851383329,  Mobile PL: +48783303040
"Simplicity is the ultimate sophistication" -- Leonardo da Vinci


More information about the Haskell-Cafe mailing list