I'm new to programming in Scala. My aim is to implement a tail-recursive program for the Towers of Hanoi problem. I believe it can be achieved by recursion like this:
// Implementing a recursive function for Towers of Hanoi,where the no of disks is taken as 'n', 'from' being the Start Peg, 'to' being the End Peg, and 'via' being Intermediate Peg
def move(n: Int, from: Int, to: Int, via: Int) : Unit = { // Recursive Function
if (n == 1) {
Console.println("Move disk from pole " + from + " to pole " + to)// each move iteration printed.
}
else {
move(n - 1, from, via, to) //Moving n-1 disks from source to Intermediate
move(1, from, to, via) // Printing all the move iterations
move(n - 1, via, to, from) //Moving n and other n-1 disks to the final destination
}
}
Can it implemented using tail-recursion as well?