Say, I got a list which length can be odd or even. Every iteration , I remove two items from the list. If there is one or no item at the end, I end the execution.
If I store (length list)/2 every loop, I will get e.g. [5,4,3...] for a list of length 10.
But what I want is [1,2,3,...] and I couldn't figure out a relationship between the list size and this sequence...
Is there a way to achieve this without having a variable storing the current count?
func :: [Int] -> [Int] only
Best How To :
You can certainly do this without changing the type signature of
func :: [Int] -> [Int]: have
func call a different function, which takes an extra argument that is the counter you were talking about:
func :: [Int] -> [Int]
func = go 0
where go _  = 
go _ [x] = 
go n (a:b:xs) = n : a : b : go (succ n) xs
If you are determined to do this without managing the counter variable yourself, you can do something more contorted, such as zipping your input list into 3-tuples
b are pairs of items from your input list and
n comes from
pairs :: [a] -> [(a,a)]
pairs  = 
pairs [_] = 
pairs (a:b:xs) = (a,b) : pairs xs
func' :: [Int] -> [Int]
func' xs = concat $ zipWith (\n (a,b) -> [n,a,b]) [1,2..] $ pairs xs
This is quite a bit less readable in my opinion, and I would suggest you just do the easy, obvious thing in my first snippet.