Last active
April 23, 2020 23:43
-
-
Save Adrian-Samuel/dba90330a1b75d731df0a3cbde6dec93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
//Option 1 | |
func recursiveLoop(list []int, currentIndex int) { | |
if !(currentIndex == len(list)) { | |
fmt.Println(list[currentIndex]) | |
recursiveLoop(list, currentIndex+1) | |
return | |
} | |
fmt.Println("Loop Finished!") | |
} | |
//Option 2 | |
func recursiveLoopDirector(list []int, reverse bool) { | |
if len(list) != 0 { | |
if reverse { | |
fmt.Println(list[len(list)-1 : len(list)][0]) | |
recursiveLoop(list[0:len(list)-1], reverse) | |
return | |
} | |
fmt.Println(list[0:1][0]) | |
recursiveLoop(list[1:len(list)], reverse) | |
return | |
} | |
fmt.Println("Loop finished") | |
} | |
func main() { | |
list := []int{1, 2, 3, 4} | |
recursiveLoop(list) // result: 1,2,3,4 | |
recursiveLoopDirector(list, true) // result: 4,3,2,1 | |
recursiveLoopDirector(list, false) // result: 1,2,3,4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment