Recursive function:
let rec listMerge (l1 : 'a list) (l2 : 'a list) =
if l1.IsEmpty then l2
elif l2.IsEmpty then l1
else l1.Head :: l2.Head :: listMerge l1.Tail l2.Tail
Now, unless I am happily mistaken, this does not actually perform tail call, it just may look like it, if not considering that ::
is right associative.
Then, I am under impression (from something I read, but couldn't find now) that this can easily be converted to tail recursive by using an extra fun
or something.
So, is it possible? Code?
My answer: So, this is how I changed the functions, thanks to answers below:
let listMerge l1 l2 =
let rec mergeLoop (l1 : 'a list) (l2 : 'a list) acc =
if l1.IsEmpty then (List.rev acc) @ l2
elif l2.IsEmpty then (List.rev acc) @ l1
else mergeLoop l1.Tail l2.Tail (l2.Head :: l1.Head :: acc)
mergeLoop l1 l2 []