Question

Been trying really hard. Don't know where to go from here ..

I need to define a function that takes an integer and returns an infinitely repeating list of numbers from 0 to n-1.

    cycle n =
        let aux1 v1 n 
            |(v1 == n) = v1 : (aux1 0 n)
            | (v1<n) = v1 : (aux1 (v1 + 1) n)
        in aux1 0 n
Was it helpful?

Solution 2

cycle' n = [0..n-1] ++ cycle' n

but i have to add some chars so stack overflow can accept such a short answer :)

OTHER TIPS

@piotrek

there is a even shorter solution:

cycle' n = [0..]>>[0..n-1]
cycle' n = xs
    where xs = take n [0 ..] ++ xs

A nice and simple way to generate a lot of infinite series is using higher-order function (map,filter, fold) on a simple stream.

For example, here's two ways of generating an infinite series of natural even numbers:

evens = filter even [1..]
evens = map (*2) [1..]

Your problem can be easily solved using this method. You might want to check out what mod does. Good luck!

I would do it like this:

cycle' n = concat $ repeat [0..n-1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top