Last active
August 29, 2015 14:04
-
-
Save ludflu/db18c3d30d74ff382676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window :: Int -> [a] -> [[a]] | |
--base case | |
window sz [] = [] | |
-- the "@" sign lets you alias the destructed variable as in "whole@(head:tail)" | |
-- "take N list" gives you the first N elements of list | |
window sz lst@(h:tl)= [take sz lst] ++ window sz tl | |
main = do | |
let seq = window 2 [1..25] | |
print "here's a 2-item sliding window" | |
print $ seq | |
print "summing over the window" | |
--sum is just "foldl (+) 0" | |
print $ map sum seq | |
--scanl doesn't make sense over a list of lists | |
--but maybe you want the intermediate values as you accumulate a total? | |
print "scanning over the window sums" | |
print $ scanl (+) 0 $ map sum seq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment