fac1 0 = 1
fac1 n = (*) n (fac1 ((-) n 1))
-- tail-recursive
fac2 0 acc = acc
fac2 n acc = fac2 ((-)n 1) ((*) acc n)
To me, something in with a pattern of:f v = g v (f (t v))
where f, g and t are functions (t is a transform on a, in the factorial case it is (-),
Can usually be turned into the form (mechanically even):
f v acc = f (t v) (g v acc)
Does this hold for all cases or am i missing something? If it does hold, could a language just implement the TCO for it, and therefore couldn't we just consider the very first implementation of fac1 to be tail-recursive?
edit: It is ugly but changed my exmple code to match the pattern i describe exactly.