[Haskell-cafe] Re: Sequence differences

Scherrer, Chad Chad.Scherrer at pnl.gov
Mon Apr 13 09:52:04 EDT 2009


 
henning thielmann writes:

> movingWindow n xs =
>     take (length xs - n +1) $ map (take n) $ tails xs
> 
> or more efficient using utility-ht package:
> 
> movingWindow n xs =
>     Data.List.Match.take (drop (n-1) xs) $
>        map (take n) $ tails xs

Oh, very nice. I was a little frustrated writing the recursion
explicitly. I guess you could also write 

movingWindow n xs = zipWith const (map (take n) $ tails xs) $ drop (n-1)
xs

Hmm, maybe this is obvious, if

Data.List.Match.take == zipWith (flip const)

(I've never used it)
 
> I'm not sure. You are safer and more efficient when you 
> restrict to pairs. 
> Since I often need the function, I defined:
>    
> http://hackage.haskell.org/packages/archive/utility-ht/0.0.4/d
oc/html/Data-List-HT.html#v%3AmapAdjacent
> 
> Then
>    diff = mapAdjacent subtract

Yes, I agree if you know you'll be using a binary operator, and not a
more general n-ary function.

Chad


More information about the Haskell-Cafe mailing list