0

Can't figure out the error in my function. It is supposed to cycle through a list n number of times, eg:

cycle([1, 2, 3, 4, 5, 6], 2) would return [3, 4, 5, 6, 1, 2], cycling the list twice.

Here is my code, but I think I am entering into an infinite recursive loop. Any help?

fun cycle (a, n) = if n >= 0 then cycle (cycle1 a, n-1)
                   else cycle (a, n-1);
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636

1 Answers1

3

You keep cycling when n < 0. To prevent looping infinitely, simply don't do that. (Also, you probably don't want to cycle1 when n = 0.)

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118