Hi all,
I'm working my way through the exercises in CTM by myself, so apologies if
you see a lot of questions about them!
Exercise 3.16 seeks a tail-recursive function for convoluting lists, and
asks that the solution convolutes two lists in the same number of recursive
calls as there are elements in the list. Convoluting the two lists [a b c]
and [1 2 3] should result in three recursive calls ultimately constructing
the list [a#3 b#2 c#1].
The question gives a hint about how the function can calculate the reverse
of the second list and use that as an input to recursive calls to itself,
since the order of unification doesn't matter.
I devised the following solution but it doesn't seem to be addressing the
meat of the issue (though it works), since the order of unification is the
standard in-order one:
declare Convolute
fun {Convolute L1 L2}
fun {ConvoluteAcc L1 L2}
case L1 of
H1|T1 then
case L2 of
H2|T2 then
H1#H2|{ConvoluteAcc T1 T2}
end
[] nil then nil
end
end
in
{ConvoluteAcc L1 {Reverse L2}}
end
What is the exercise trying to get me to see? That I should somehow
incrementally calculate the reverse of the second list in each recursive
call? I've rolled my own Reverse function for lists below but I'm not seeing
how to adapt that structure to the above problem...
declare MyReverse
fun {MyReverse L}
fun {ReverseAcc L Acc}
case L of H|T then {ReverseAcc T H|Acc}
[] nil then Acc
end
end
in
{ReverseAcc L nil}
end
Thanks,
Oliver.
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users