slide: useful function?
I want to propose the following function slide, which is like map, but depends not on one value of a list, but on several consecutive ones. slide :: ([a] -> b) -> [a] -> [b] slide f [] = [] slide f xs = f xs : slide f (tail xs) For example, this is useful for a digital filter, like lowpass = firfilter [0.005178,0.005712,0.00589,0.005712,0.005178] firfilter :: [Coeff] -> DigitalFilter firfilter b = slide (firfilter' b) where firfilter' b = sum . zipWith (*) b What do you think? Or is there already a function slide, just with another name? Markus -- Markus Schnell, Infineon Technologies AG
Doesn't seem that usefull to me, you can get the several consecutive ones by applying tails to your list.
I want to propose the following function slide, which is like map, but depends not on one value of a list, but on several consecutive ones.
slide :: ([a] -> b) -> [a] -> [b] slide f [] = [] slide f xs = f xs : slide f (tail xs)
slide f = map f.init.tails The init is needed because you don't apply f to the empty List. If you did, then it would be just.
slide :: ([a] -> b) -> [a] -> [b] slide f [] = f [] <--------------- slide f xs = f xs : slide f (tail xs)
slide f = map f.tails J.A.
Markus.Schnell@infineon.com wrote:
I want to propose the following function slide, which is like map, but depends not on one value of a list, but on several consecutive ones.
slide :: ([a] -> b) -> [a] -> [b] slide f [] = [] slide f xs = f xs : slide f (tail xs)
The function is interesting from the theoretical side since it is an instance of *redecoration*. Written as slide :: [a] -> ([a] -> b) -> [b] , one sees a similarity with the monadic multiplication (or substitution, here for the case of lists)
= :: [a] -> (a -> [b]) -> [b] .
Indeed, slide is the multiplication operation of lists viewed as a *comonad* and hence the dual of ">>=". Read more in T Uustalu, V Vene. The dual of substitution is redecoration. In K Hammond, S Curtis, eds, Trends in Functional Programming 3, pp 99-110. Intellect, Bristol / Portland, OR, 2002. - .ps.gz, 46K (© Intellect) http://www.cs.ioc.ee/~tarmo/papers/sfp01-book.ps.gz , if you want to be bothered with categorical nonsence ;-) Cheers, Andreas -- Andreas Abel --<>-- What if Jesus is right? Theoretical Computer Science, University of Munich http://www.tcs.informatik.uni-muenchen.de/~abel/
participants (3)
-
Andreas Abel -
Jorge Adriano -
Markus.Schnell@infineon.com