如何中止对输入序列的求值? [英] How to abort evaluation of a sequence of inputs?

查看:64
本文介绍了如何中止对输入序列的求值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,在评估中按 Alt + .或调用Abort[]会导致当前评估输入中止.但是在FrontEnd中工作时,我们通常会向内核发送一系列输入.例如,如果我们在一个Cell中的不同行上键入以下三个表达式,然后按 Shift + Enter ,我们将得到无限求值:

By default pressing Alt+. or calling Abort[] within the evaluation causes abort of the currently evaluating input. But when working in the FrontEnd we usually send to the kernel a sequence of inputs. For example, if we type the following three expressions on separate lines in one Cell and then press Shift+Enter we get infinite evaluation:

f := CheckAbort[Pause[.1], Abort[]]
While[True, f]
While[True, f]
While[True, f]

要停止此无限评估,我们必须按 Alt + .三次.

To stop this infinite evaluation we must to press Alt+. three times.

如何在上面的示例中定义函数f,方法是按 Alt + . 一次将中止是否在不退出内核的情况下评估输入的全部顺序?

How to define the function f in the example above in such a way that pressing Alt+. one time will abort the evaluation of the full sequence of inputs without quitting the kernel?

编辑

我认为,如果FrontEnd为MathKernel创建输入队列,它可能也可以取消此队列.

I think that if the FrontEnd creates an input queue for MathKernel it probably can also cancel this queue.

推荐答案

Sasha仅使用四个单元格的建议是有效的,因为这基本上就是FrontEnd所做的,向内核提交了多个不同的评估.如果您坚持使用一个单元格,则将其包装在括号中(CompoundExpression),这会将这四行视为一个求值(也需要使用;):

Sasha's suggestion to just use four cells is valid, because that's basically what the FrontEnd does anyway, submitting several distinct evaluations to the Kernel. If you insist in using one cell, wrap it in parens (CompoundExpression), which causes those four lines to be treated as one evaluation (note the need for ; too):

(
   f := CheckAbort[Pause[.1], Abort[]];
   While[True, f];
   While[True, f];
   While[True, f]
)

然后,发出一个中止操作将中止整个评估.

Then, one abort issued will abort the evaluation as a whole.

这篇关于如何中止对输入序列的求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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