Proposal reminder: Add functions to get consecutive elements to Data.List

Henning Thielemann lemming at henning-thielemann.de
Thu May 19 07:21:14 UTC 2016


On Thu, 19 May 2016, Henning Thielemann wrote:

> On Thu, 19 May 2016, David Feuer wrote:
>
>> You promised a collection of use cases. I seem to have missed it. Could you 
>> send the link again?
>
>
> I found the following uses in my libraries:
>
> * mapAdjacent subtract
>    differences between consecutive elements, inverse of cumulative sum 
> (scanl (+) 0)
>
> * and . mapAdjacent (==)
>    check whether all elements in a list are equal
>
> * and . mapAdjacent (<=)
>    check whether elements are sorted
>
> * mapAdjacent (/=) . map signum
>    find zero crossings
>
> * head . dropWhile (uncurry (/=)) . mapAdjacent (,)
>    drop until convergence
>
> * mapAdjacent (\x y -> (snd x, fst y))
>    turn list of intervals into list of gaps
>
> * mapAdjacent (,)
>    collect state transitions for a Hidden Markov model
>
> * product . mapAdjacent binomial . scanr1 (+)
>    compute multinomial coefficient


I found another application: Compute the longest duplicated string.
    https://programmingpraxis.com/2010/12/14/longest-duplicated-substring/

import Data.List
import Data.List.HT (mapAdjacent)
import qualified Data.List.Key as K

lds :: Ord a => [a] -> [a]
lds = K.maximum length . mapAdjacent lcp . sort . tails where
     lcp (x:xs) (y:ys) | x == y = x : lcp xs ys
     lcp _      _               = []


More information about the Libraries mailing list