[Haskell-cafe] defining mapPairs function
twifkak at comcast.net
Thu Aug 30 10:05:44 EDT 2007
That's great (really, thank you for such a fun example of Arrow
programming), but isn't the (*) on line two of mapPair supposed to be a
"point"? How would you make a point-less version of mapPair that
actually had the type signature (a->a->a)->[a]->[a]*? (For that matter,
* Grr, you're right. Were it not for that odd requirement, the type
could be loosened to (a->a->b)->[a]->[b]. Maybe mapPairs should take a
monadic (that is, one-arg) function to handle the dangling oddies.
Dan Weston wrote:
> import Control.Arrow((&&&),(>>>))
> import Data.Maybe(catMaybes,maybeToList)
> mapPair = (id &&& tail >>> -- offset list by one
> uncurry (zipWith (*)) >>> -- multiply adjacent
> alternate >>> -- mark even elements
> catMaybes) -- delete even elements
> &&& -- Tuple this up with...
> (alternate >>> -- keep odd indices
> (Nothing:) >>> -- make sure there is a last
> last >>> -- get last
> maybeToList) -- keep if it had odd index
> >>> -- and then...
> uncurry (++) -- append pair of lists
> where alternate = zipWith ($) (cycle [Just,const Nothing])
> -- Mark even-indexed elements for deletion
> -- cycle goes on forever, but zipWith stops at
> -- the end of the shorter list, so no worries.
More information about the Haskell-Cafe