[Haskell-cafe] Applying different functions to different types
for lists
Ryan Ingram
ryani.spam at gmail.com
Thu Jul 16 17:31:13 EDT 2009
2009/7/16 Szekeres István <szekeres at iii.hu>:
> class ListUpdater a where
> updateFn :: Char -> Char
> update :: a -> a
>
> so I can define the update function for the different types of lists:
>
> instance ListUpdater String where
> update = map updateFn
>
> instance ListUpdater L.ByteString where
> update = L.pack . (map updateFn) . L.unpack
So, as written, these functions both give an error, because updateFn
isn't defined.
Perhaps you want something like this:
> class CharMap a where
> charMap :: (Char -> Char) -> (a -> a)
> instance CharMap Char where
> charMap f = f
> instance CharMap a => CharMap [a] where
> charMap f = map (charMap f)
> instance CharMap L.ByteString where
> charMap f = L.pack . map f . L.unpack
Now you can write
> genericRot13, genericUL :: CharMap a => a -> a
> genericRot13 = charMap rot13
> genericUL = charMap ul
-- ryan
More information about the Haskell-Cafe
mailing list