我如何跳出Lisp中的功能? [英] How do I jump out of a function in Lisp?

查看:153
本文介绍了我如何跳出Lisp中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(通用)Lisp中是否可以跳转到另一个函数而不是调用另一个函数? 我的意思是,当前函数已损坏,并且调用了另一个函数,而没有跳回成千上万个函数,就好像我要确定自己是否完成了 tail调用优化,即使它不是最后一个. 我不确定是否要执行((fn x的返回)).

Is it possible in (Common) Lisp to jump to another function instead of call another? I mean, that the current function is broken and another is called, without jumping back through thousands of functions, as if I'd decide myself if tail call optimization is done, even if it is not the tail. I'm not sure if "(return-from fn x)" does, what I want.

示例:

(defun fn (x)
  (when x
    (princ x)
    (jump 'fn (cdr x)))
  (rest))

'jump'应该类似于在不保存该函数位置的情况下调用以下函数,而是返回至原始funcall所在的位置,这样就不会出现堆栈溢出. 仅当x为nil时才执行休息".

'jump' should be like calling the following function without saving the position of this function, instead returning to, where the original funcall was, so that there will be no stack overflow. 'rest' should only be executed if x is nil.

推荐答案

您如何使用尾部呼叫?

(defun fn (x)
  (if x
      (progn
        (princ x)
        (fn (cdr x)))
      (progn
        (rest))))

它在尾部位置调用fn.如果实现提供了尾部调用优化,则不会导致堆栈溢出.如果您不想依赖于此,则需要以非递归的方式处理该问题.在Common Lisp中没有明确的删除此函数堆栈框架,然后调用函数X"运算符.

It calls fn in a tail position. If an implementation provides tail call optimization, you won't get a stack overflow. If you don't want to rely on that, you would need to handle the problem in a non recursive way. There are no explicit 'remove this functions stack frame and then call function X' operators in Common Lisp.

这篇关于我如何跳出Lisp中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆