Applying different functions to different types for lists

Hi, I want to build a framework to apply different functions to different types of lists and I want to generalize the solution as much as possible but my Haskell knowledge is not really enough to do this (I'm a haskell novice). The problem has two dimensions: 1. Lets say I have two functions: r13, ul : Char -> Char where r13 returns the rot13ed character, and ul converts upper case chars to lower case and vice versa. Later I want a rot13 function that can operate on different types of lists and an uxl function that can also operate on different types of lists. 2. Lets say I have two kinds of lists to handle: String and Data.ByteString.Lazy as L. For Strings I want the updater function to be used for each character and for L I want all items to be converted to Char, the updater function to be updated and the result to be converted back to the original item type. Therefore I define a common class for handling these lists: 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 But now how should I glue these 2 dimensions together? How to say that I want a rot13 function that applies to any data type instantiated from ListUpdater and that is using the r13 function to do the update? And also that I want an uxl function that also applies to any data type instantiated from ListUpdater and that is using the ul function to do the update? Thanks, Istvan

2009/7/16 Szekeres István
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
participants (2)
-
Ryan Ingram
-
Szekeres István