
Hi, I'm trying to write a generalization of the map function that operates on nested lists, e.g. [[a]] or [[[a]]]. (It should map all elements of type a (not list) with f) I was thinking of something along these lines: mapN f *list of type [a] (where a is not a list type)* = map f l mapN f l = (mapN f (head l)):(mapN f (tail l)) Any suggestions about how to go about this? Many thanks, Ben -- Ben Derrett Department of Mathematics MIT Class of 2012 bderrett@mit.edu

I'm a beginner too but, if I am not wrong, then you will not be able to
write a function that can receive input of type [[a]] as well as input of
type [[[a]]].
Is that the behaviour that you have in mind?
On Thu, Apr 8, 2010 at 09:08, Ben Derrett
Hi, I'm trying to write a generalization of the map function that operates on nested lists, e.g. [[a]] or [[[a]]]. (It should map all elements of type a (not list) with f)
I was thinking of something along these lines: mapN f *list of type [a] (where a is not a list type)* = map f l mapN f l = (mapN f (head l)):(mapN f (tail l))
Any suggestions about how to go about this?
Many thanks,
Ben
-- Ben Derrett Department of Mathematics MIT Class of 2012 bderrett@mit.edu
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- mac

You can't write a single such function to operate on all the different types, but you can use Conal's semantic editor combinators. In the case of something to map onto the elements of a list of lists, you'd want (map . map), or (fmap . fmap) to generalise things slightly. In the case of lists of lists of lists, you need (fmap . fmap . fmap). Note that you can generalise this to for example applying to the elements of lists which are embedded in the first element of a tuple: first . fmap. Or a list of Maybe tuples containing lists in their second element: fmap . fmap . fmap . fmap Bob On 8 Apr 2010, at 14:16, matthew coolbeth wrote:
I'm a beginner too but, if I am not wrong, then you will not be able to write a function that can receive input of type [[a]] as well as input of type [[[a]]].
Is that the behaviour that you have in mind?
On Thu, Apr 8, 2010 at 09:08, Ben Derrett
wrote: Hi, I'm trying to write a generalization of the map function that operates on nested lists, e.g. [[a]] or [[[a]]]. (It should map all elements of type a (not list) with f) I was thinking of something along these lines: mapN f *list of type [a] (where a is not a list type)* = map f l mapN f l = (mapN f (head l)):(mapN f (tail l))
Any suggestions about how to go about this?
Many thanks,
Ben
-- Ben Derrett Department of Mathematics MIT Class of 2012 bderrett@mit.edu
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- mac _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 8 April 2010 14:37, Thomas Davie
You can't write a single such function to operate on all the different types, .... [SNIP]
Hi Bob (?) I believe Oleg Kiselyov's deepset functor manages it... http://okmij.org/ftp/Haskell/typecast.html http://okmij.org/ftp/Haskell/deepest-functor.lhs PS - is it Bob or Thomas? thanks. Best wishes Stephen

On 8 Apr 2010, at 16:34, Stephen Tetley wrote:
On 8 April 2010 14:37, Thomas Davie
wrote: You can't write a single such function to operate on all the different types, .... [SNIP]
Hi Bob (?)
I believe Oleg Kiselyov's deepset functor manages it...
http://okmij.org/ftp/Haskell/typecast.html http://okmij.org/ftp/Haskell/deepest-functor.lhs
PS - is it Bob or Thomas? thanks.
Well corrected, and either. Ta Bob

2010/4/8 Ben Derrett
Hi, I'm trying to write a generalization of the map function that operates on nested lists, e.g. [[a]] or [[[a]]]. (It should map all elements of type a (not list) with f) I was thinking of something along these lines: mapN f *list of type [a] (where a is not a list type)* = map f l mapN f l = (mapN f (head l)):(mapN f (tail l))
Any suggestions about how to go about this? Many thanks, Ben
I think you should look into generic programming for doing this, e.g. "Scrap Your Boilerplate". There are (at least) three papers on it[1]. [1]: http://research.microsoft.com/en-us/um/people/simonpj/Papers/hmap/index.htm -- Deniz Dogan
participants (5)
-
Ben Derrett
-
Deniz Dogan
-
matthew coolbeth
-
Stephen Tetley
-
Thomas Davie