I cannot make it work in Dyalog APL
solve←{
n a c b←⍵
n≤0:⍬
solve(n-1)a b c
⎕←'Move disk from' a 'to' c
solve(n-1)b c a
}
solve 4 'A' 'C' 'B'
It loops from the first solve (n-1) a b c
but never goes to line 4.
The same code works in JavaSCript:
solve = (n, a, c, b) => {
if (n <= 0) return
solve(n-1, a, b, c)
console.log(`Move disk from ${a} to ${c}`)
solve(n-1, b, c, a)
}
solve(4, 'A', 'C', 'B')
When I print the input params it shows:
solve←{
n a c b←⍵
⎕←n a c b
n≤0:⍬
solve(n-1)a b c
⎕←'Move disk from' a 'to' c
solve(n-1)b c a
}
4 ACB
3 ABC
2 ACB
1 ABC
0 ACB
Any ideas?