1

The following code is intended to append two lists.

fun {AppendLists L1 L2}
    if L1 == nil then L2
    else
        L1.1 | {AppendLists L1.2 L2}
    end
end
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

2 Answers2

1

If you don't see why your code is already tail-recursive, here's the same code with a bit less syntactic sugar. Your function as been converted to a procedure with an extra argument to store the result (this is denoted by a facultative '?').

proc {AppendLists L1 L2 ?R} 
  if L1 == nil then L2
  else K in 
     R = L1.1 | K
     {AppendLists L1 L2 K}
  end
end 
francoisr
  • 4,407
  • 1
  • 28
  • 48
0

This code is already tail-recursive. You have recursion as the last statement, so you are calculating the result first, and then call the recursion.

rok
  • 2,574
  • 3
  • 23
  • 44