如何在F#中实现goto? [英] How do you implement goto in F#?

查看:90
本文介绍了如何在F#中实现goto?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有喜欢的语言都有一个goto命令.也就是说,您可以创建一个标签,然后在以后中断程序流程以转到该标签.此构造的更有用的应用程序之一是创建一个无限循环,如下所示:

All my favorite languages have a goto command. That is, you can create a label, and then later interrupt the flow of the program to go to the label. One of the more useful applications of this construct is to create an infinite loop, like this:

 start:
 goto start

不幸的是,如果我正确理解了编译器错误,则无法在F#中使用相同的语法.因此,由于它似乎不受本机支持,因此如何在F#中实现goto命令?

Unfortunately, if I undertstand the compiler errors correctly, I can't use this same syntax in F#. So, since it doesn't seem to be supported natively, how can I implement the goto command in F#?

当然,F#是一种功能强大的语言,足以实现如此简单的功能.其他语言(例如Javascript)本身不支持goto,它们仍然可以通过插件仍然能够实现内.

Surely, F# is a powerful enough language to implement a feature as simple as this. Other languages, such as Javascript, which do not natively support goto, are still able to implement it through a plug-in.

此外,我认为F#作为功能编程范例中的一种语言,应该能够支持更高级别的goto s:您可以在其中将goto s传递给goto s.

Further, I feel that F#, as one of the languages in the functional programming paradigm, should be able to support higher-level gotos: where you can pass gotos to gotos.

推荐答案

标签与函数有很多共同点:它们都充当要执行的某些代码的入口点.鉴于相似之处,您可以执行以下操作:

A label has a lot in common with a function: they both act as entry points to some code to be executed. Given that similarity, you can do the following:

let goto f = f()

let test() =
  let label = (fun () ->
    //rad code
    )
  //tight code
  goto label

一个小缺点是必须将所有代码包装在闭包中.我不知道-拿到像goto这样方便的东西似乎还不错.

One minor drawback is having to wrap all your code in closures. I don't know--doesn't seem too bad for getting something as handy as goto.

这篇关于如何在F#中实现goto?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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