How to repeat the list of functions in haskell

I want to have an endless list of functions that go through some pattern. For example: [(+), (-), (+), (-), ...]

If I'm something like

 fmap repeat [(+), (-)] 

then I get nested lists [[a -> a -> a]] . What is the best way to get one endless list of functions following a pattern?

+5
source share
1 answer

What you are looking for is cycle :: [a] -> [a] :

 cycle [(+),(-)] 

Type of this expression:

 Prelude> :t cycle [(+),(-)] cycle [(+),(-)] :: Num a => [a -> a -> a] 

cycle takes a list [a] and creates a list in which the given list repeats over and over. So cycle [1,2,3] produces [1,2,3,1,2,3,1,2,3,1,...]

+13
source

Source: https://habr.com/ru/post/1270816/


All Articles