Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created December 9, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/ffecfa66c181e03075d9395f9fc433e7 to your computer and use it in GitHub Desktop.
Save derrickturk/ffecfa66c181e03075d9395f9fc433e7 to your computer and use it in GitHub Desktop.
Sliding-windows in Haskell
takeExactly :: Int -> [a] -> Maybe [a]
takeExactly 0 _ = Just []
takeExactly n [] = Nothing
takeExactly n (x:xs) = (x:) <$> takeExactly (n - 1) xs
windows :: Int -> [a] -> [[a]]
windows n xs = case takeExactly n xs of
Nothing -> []
Just window -> window:windows n (tail xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment