[Haskell-cafe] Does somebody know about these functions?

Johan Holmquist holmisen at gmail.com
Tue Feb 28 18:06:25 CET 2012

Two functions that I see useful are described here and I would like to
know if they are defined in some more or less standard Haskell
library. Hoogle (http://www.haskell.org/hoogle) did not reveal
anything about that.

Function 'inter' applies given function to each succeeding pair of
elements of a list.

inter :: (a -> a -> b) -> [a] -> [b]
inter f [] = []
inter f l  = map (uncurry f) $ zip l (tail l)

Example usage:

  and $ inter (<=) l   -- checks if 'l' is ordered

  inter (,) l   -- gives succeeding pairs

Function 'withPair' takes a pair and applies a function to it's first
element, another function to it's second element and finally combines
the results with yet another function.

withPair :: (a' -> b' -> c) -> (a -> a') -> (b -> b') -> (a,b) -> c
withPair f fa fb (a,b) = fa a `f` fb b

Example usage:

  words [] = []
  words s  = withPair (:) id words (break isSpace $ dropWhile isSpace s)

  lines [] = []
  lines s  = withPair (:) id lines (break (== '\n') s)

  mapPair = withPair (,)

This function can abstract away the (in my opinion ugly) pattern (as
seen in the examples):

  foo list = let (a,b) = <somefun> list in <somefun2> a `<combinedWith>` foo b

Anyone knows about these two functions or variants thereof?


More information about the Haskell-Cafe mailing list