
Hello libraries, similar to mapFst and Co, we can define: -- |Map various parts of list mapHead f [] = [] mapHead f (x:xs) = f x : xs mapTail f [] = [] mapTail f (x:xs) = x : map f xs mapInit f [] = [] mapInit f xs = map f (init xs) : last xs mapLast f [] = [] mapLast f xs = init xs : map f (last xs) btw, two last operations may be a bit slow. it's possible to define initLast function that returns both parts and still lazy? -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On 11/4/06, Bulat Ziganshin
Hello libraries,
similar to mapFst and Co, we can define:
-- |Map various parts of list
I don't think this is really that useful. Lists are homogeneous,
tuples are heterogeneous.
--
Taral

On 11/4/06, Taral
On 11/4/06, Bulat Ziganshin
wrote: Hello libraries,
similar to mapFst and Co, we can define:
-- |Map various parts of list
I don't think this is really that useful. Lists are homogeneous, tuples are heterogeneous.
Hmm, from the first two lines, I was expecting mapHead :: (a -> a) -> [a] -> [a] as given by Bulat, but also mapTail :: ([a] -> [a]) -> [a] -> [a] instead of mapTail :: (a -> a) -> [a] -> [a] I wasn't expecting the other two at all, but I would have expected mapInit to have a similar type to mapTail. (But really, I wouldn't have wanted those. Not that I'm sure I'd want either of the first two.)

Hello Samuel, Sunday, November 5, 2006, 3:02:29 AM, you wrote:
I don't think this is really that useful. Lists are homogeneous, tuples are heterogeneous.
Hmm, from the first two lines, I was expecting mapTail :: ([a] -> [a]) -> [a] -> [a]
well, it was not really good thought. i just wrote mapHead function that used to capitalize first char of line: print$ (first_time &&& mapHead toUpper) str ++... and thought that other sorts of map* functions may be also useful at least i disagree that lists should be always considered as homogeneous, just look at foldr1 but anyway, it was crush&burn. if you that it is bad - so it is -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hi
-- |Map various parts of list mapHead f [] = [] mapHead f (x:xs) = f x : xs
concatMap is concat and map intercat/consperse is concat and intersperse mapSnd is map and snd surely if you heard of mapHead, then you'd expect it to be defined as: mapHead = map head ? Thanks Neil

On 11/4/06, Neil Mitchell
mapSnd is map and snd
Actually, mapSnd is ($) and snd, which is why I always preferred the names from Control.Arrow (first and second) -- they don't include a reference to a fictional mapping operation. For the same reason, I would at the very least oppose mapHead and mapLast because there's no mapping going on. /g

"J. Garrett Morris"
On 11/4/06, Neil Mitchell
wrote: mapSnd is map and snd
Actually, mapSnd is ($) and snd, which is why I always preferred the names from Control.Arrow (first and second) -- they don't include a reference to a fictional mapping operation.
Well, you could call it fmapSnd if you preferred, using fmap at the identity instance of Functor. The concept of map (fmap) is more general than just lists! It is about type conversion (a->b) embedded through all relevant locations in a data structure. Regards, Malcolm

Malcolm Wallace schrieb:
The concept of map (fmap) is more general than just lists!
Right! It's recursive only for recursive data structures. In the case of tuples mapping degenerates or falls together with a kind of record update. Also the proposal for mapHead is a kind of record update, whereas it is unclear what mapTail should be. In fact the compiler should be able to systematically generate/derive update and/or map functions from data types (however such functions might be named). Christian

Hello Neil, Sunday, November 5, 2006, 3:08:24 AM, you wrote:
concatMap is concat and map intercat/consperse is concat and intersperse mapSnd is map and snd
surely if you heard of mapHead, then you'd expect it to be defined as:
mapHead = map head
just at the moment when you will define concatMap = concat Map and mapSnd = map snd :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Sat, 4 Nov 2006, Bulat Ziganshin wrote:
Hello libraries,
similar to mapFst and Co, we can define:
-- |Map various parts of list mapHead f [] = [] mapHead f (x:xs) = f x : xs
mapTail f [] = [] mapTail f (x:xs) = x : map f xs
mapInit f [] = [] mapInit f xs = map f (init xs) : last xs
mapLast f [] = [] mapLast f xs = init xs : map f (last xs)
btw, two last operations may be a bit slow. it's possible to define initLast function that returns both parts and still lazy?
In order to make such operations more efficient I defined: {- | It holds @splitLast xs == (init xs, last xs)@, but 'splitLast' is more efficient if the last element is accessed after the initial ones, because it avoids memoizing list. -} splitLast :: [a] -> ([a], a) splitLast [] = error "splitLast: empty list" splitLast [x] = ([], x) splitLast (x:xs) = let (xs', lastx) = splitLast xs in (x:xs', lastx) propSplitLast :: Eq a => [a] -> Bool propSplitLast xs = splitLast xs == (init xs, last xs)
participants (8)
-
Bulat Ziganshin
-
Christian Maeder
-
Henning Thielemann
-
J. Garrett Morris
-
Malcolm Wallace
-
Neil Mitchell
-
Samuel Bronson
-
Taral