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

Krzysztof Kościuszkiewicz k.kosciuszkiewicz at gmail.com
Fri Sep 28 10:55:34 EDT 2007


Fellow Haskellers,

I wanted to experiment a bit with lists and sequences (as in Data.List and
Data.Sequence), and I got stuck. I wanted to dynamically select processing
function depending on cmdline argument:

> main = do
>     args <- getArgs
>     let reversor = case args of
>             ["sequence"] -> reverseList
>             ["list"] -> reverseSeq
>             _ -> error "bad args"
>     input <- getContents
>     let output = reversor $ lines $ input
>     mapM_ putStrLn output

In my oppinion reversor would have type

> reversor :: (Foldable f) => [a] -> f b

but I couldn't get this to work. I've tried typeclass approach:

> class (Foldable f) => Reversor f where
>     reverse' :: [a] -> f a
> 
> instance Reversor ([]) where
>     reverse' = Data.List.reverse
> 
> instance Reversor ViewR where
>     reverse' = viewr . foldr (<|) empty 
>
> reverseList = reverse' :: (???)
> reverseSeq  = reverse' :: (???)

but now in order to differentiate between "reverse'" functions I'd
have to provide different type annotations, and then "reversor" won't
typecheck...

Similar problem surfaced with this try:

> data Proc = SP | LP
> reverseList = reverse' LP
> reverseSeq = reverse' SP
>
> reverse' :: (Foldable f) => Proc -> [a] -> f a
> reverse' LP = Data.List.reverse
> reverse' SP = viewr . foldr (<|) empty

So now I'm looking for some suggestions how should I approach the
problem...

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