Function to get NGrams fast in Haskell Is this a good way to get all sublists of a sequence/list that have a given length?

An inefficient way to do it would be something like

f n = (filter (\x -&gt; (length x) &gt; n)) . (take n) . tails


This just takes the n first elements of each tail of the original list. I think it should be slow because of the length check on every tail-element.

A smarter way would should be to "slide" a sequence of length n over an input sequence and save the result of each slide by one to the right.

-- | Get all the subsequences of a given sequence sq of length n
ngrams::Int -&gt; Seq a -&gt; Seq (Seq a)
ngrams n sq | length sq &lt; n = empty
ngrams n sq | otherwise = ngrams' restSequence initialWindow empty
            where
              initialWindow = take n sq
              restSequence  = drop n sq
              ngrams' (viewl -&gt; EmptyL) window acc = acc |&gt; window
              ngrams' (viewl -&gt; x :&lt; s) window@(viewl -&gt; a :&lt; r) acc = 
                                            ngrams' s (r |&gt; x) (acc |&gt; window)


Somehow I have the feeling I am missing an obvious way to do this better...
A little math goes a long way.

ngrams :: Int -&gt; [a] -&gt; [[a]]
ngrams n l = take (length l - (n - 1)) . map (take n) . tails $ l