|
ngrams from the book NLP for the Working Programmer: ngrams' :: Int -> [b] -> [[b]]
ngrams' n = filter ((==) n . length) . map (take n) . tails
ghci session: λ> let ngrams' n = filter ((==) n . length) . map (take n) . tails
λ> inputList
["all","this","happened","more","or","less"]
λ> tails inputList
[["all","this","happened","more","or","less"],["this","happened","more","or","less"],["happened","more","or","less"],["more","or","less"],["or","less"],["less"],[]]
λ> map (take 2) (tails inputList)
[["all","this"],["this","happened"],["happened","more"], ["more","or"],["or","less"],["less"],[]]
λ> filter ((==) 2 . length) (map (take 2) (tails inputList))
[["all","this"],["this","happened"],["happened","more"],["more","or"],["or","less"]]
Pointfree Haskell code oftens ends up being a lot like piping together Unix commands.Except typed and pure. With global type inference, so you have an objective type in mind, you can slap together and query the type with :t in ghci and see if it looks like what you wanted. |