Why can't generic function templates use fnx to ensure they are tail-recursive?

可紊 提交于 2019-12-11 07:49:22

问题


Just the title, really.

Compiling

fnx {a:t@ype} repeat {n:nat} .<n>.
( x: a
, t: int n
, f: a -> a
) : a =
  if t = 0 then x
  else repeat (f x, t - 1, f)

gives

warning(2): [fnx] is treated as [fun] for initiating function templates!

Am i supposed to take it on faith that I didnt screw up the definition and accidentally made a non-tail-recursive function? Is there some standard way to circumvent this?


回答1:


Note that 'fnx' is only meaningful for defining mutually recursive functions.

In your example, using 'fun' is just fine: the compiler can recognize the tail-recursive call in the body of 'repeat' and turn it into a local jump.



来源:https://stackoverflow.com/questions/57446917/why-cant-generic-function-templates-use-fnx-to-ensure-they-are-tail-recursive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!