For example, I need a function:
gather :: Int -> [a] -> [[a]] gather n list = ???
where gather 3 "Hello!" == ["Hel","ell","llo","ol!"].
gather 3 "Hello!" == ["Hel","ell","llo","ol!"]
I have a working implementation:
gather :: Int-> [a] -> [[a]] gather n list = unfoldr (\x -> if fst x + n > length (snd x) then Nothing else Just (take n (drop (fst x) (snd x)), (fst x + 1, snd x))) (0, list)
but I wonder if there is something already built into the language for this? I looked at Data.List but didn't see anything.
You can use tails:
tails
gather n l = filter ((== n) . length) $ map (take n) $ tails l
or using takeWhileinstead filter:
takeWhile
filter
gather n l = takeWhile ((== n) . length) $ map (take n) $ tails l
EDIT: you can remove the filter step by discarding the last nelements of the list returned from tails, as suggested in the comments:
n
gather n = map (take n) . dropLast n . tails where dropLast n xs = zipWith const xs (drop n xs)
Tail drop can be arranged automatically, thanks to the zipping properties,
import Data.List (tails) g :: Int -> [a] -> [[a]] g n = foldr (zipWith (:)) (repeat []) . take n . tails
transpose . take n . tails. :
transpose . take n . tails
Data.List > g 3 [1..10] [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [ 7,8,9], [8,9,10]]Prelude Data.List > . 3. $[1..10][[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [ 7,8,9], [8,9,10], [9,10], [10]]
Source: https://habr.com/ru/post/1659982/More articles:No system links Xamarin.Forms.NetStandard - c #Xamarin Forms.NETStandard 1.4 Resource Issue - c #c custom netcat for a simple web server - cHaskell moving average - arraysFirebase Ion / Cord Autonomy - cordovaLLMNR (Local Multicast Name Resolution) Java Responder - javaPowerShell crashes with `if ($? = $ False)` - powershellPhoenix programming: undefined function page_path / 2 - elixirFind the longest route in a 2D array - algorithmhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1659987/another-git-process-seems-to-be-running-and-thus-cant-commit&usg=ALkJrhhI3u13ZYyu3q-YrBAtsKg6adhIMQAll Articles