Skip to content

Instantly share code, notes, and snippets.

@ludflu
Last active August 29, 2015 14:04
Show Gist options
  • Save ludflu/db18c3d30d74ff382676 to your computer and use it in GitHub Desktop.
Save ludflu/db18c3d30d74ff382676 to your computer and use it in GitHub Desktop.
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