为什么要“在...期间做"?在F#中不存在 [英] Why "do...while" does not exist in F#

查看:57
本文介绍了为什么要“在...期间做"?在F#中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在...时……"

I cannot find "do...while..."

我必须这样编码:

let bubbleSort a=
    let n = Array.length a
    let mutable swapped = true
    let mutable i = 0
    while swapped do
        swapped <- false
        for j = 0 to n-i-2 do
            if a.[j] > a.[j+1] then
                let t = a.[j]
                a.[j] <- a.[j+1]
                a.[j+1] <- t
                swapped <- true
        i <- i+1

如果没有"同时...做",则代码是错误的.
可悲的是,"中断/继续"也不可用.

The code is bad without "do...while".
Sadly, "break/continue" are also not available.

推荐答案

F#非常适合非功能性编程.实际上,能够以命令式的方式微调算法的某些部分是该语言的主要优势之一.

F# is very much suitable for non-functional programming. In fact, being able to fine-tune parts of an algorithm in an imperative style is one of the major strong points of the language for me.

例如,在解决项目欧拉问题时,我从使用不可变的集和折叠的干净的功能性解决方案开始.花了150秒才能完成.现在,有了我的算法框架,我就可以一次分解数据结构并折叠一次操作,直到我将运行时间缩短到5秒.我的最终解决方案非常有必要(甚至比等效的C#版本要快).

For example, in tackling a project euler problem, I started out with a clean functional solution using immutable sets and folds. It took 150 seconds to complete. Now having the framework of my algorithm in place allowed me to pick apart the data structures and folds operations one at a time until I managed to get the run time down to 5 seconds. My final solution was very much an imperative one (and even slightly faster than an equivalent C# version).

如您所见,我首先通过以功能样式编码解决方案,然后将小部分重写为命令式样式来解决了该问题.不必处理索引和其他循环条件,就可以使代码对我更容易理解.

As you can see I solved it by coding a solution in functional style first and then rewrite small parts to an imperative style. Not having to deal with indices and other loop conditions explicitly kept the code more understandable for me.

一旦您学会了如何像函数程序员一样思考,您会发现您很少需要中断和继续.那就是我的经历.但是,如果确实需要它们,则知道如何以功能方式进行思考有助于提出解决方法,通常会涉及到以前是循环的尾部递归版本.

Once you learn how to think like a functional programmer you'll find that you'll rarely want breaks and continues. That's what I experienced. But if you do need them, knowing how to think in a functional way helps in coming up with work-arounds, usually involving a tail-recursive version of what used to be a loop.

当您开始以惯用的F#方式进行更多思考时,您可能会看到越来越多的(tail-)递归代码代替了您以前使用循环结构所做的事情.哎呀,到目前为止,写F#已有2年了,这使我心烦意乱,以至于我更有可能选择递归并折叠循环.

By the time you start thinking more in an idiomatic F# way, you'll probably see more and more (tail-)recursive code replacing what you used to do with looping constructs. Heck, writing F# for 2 years now has warped my mind so far that I'm more likely to pick recursion and folds over loops.

每当我认为需要中断/继续时,我通常就不需要,因为隐藏了一个更干净的算法版本并等待退出.最大的挑战是学习如何找到更清洁的版本.恐怕大量的实践和良好的榜样是使您更好地进行功能思考的唯一途径,但我相信这是一项值得花费的努力.

Whenever I think I need break/continue, I usually don't because there's a cleaner version of the algorithm hidden and waiting to get out. The biggest challenge is learning how to find that cleaner version. I'm afraid that lots of practice and good examples are the only way to get better at thinking functionally, but I believe that it's an effort well spent.

具有讽刺意味的是,冒泡排序是一种实际上为具有可变内容的数组而设计的算法.与命令式版本相比,任何递归气泡式排序都可能更难理解.我想我只是在这里杀了我自己的帖子.

ironically, bubble sort is an algorithm which is actually designed for arrays with mutable contents. Any recursive bubble sort is likely to be harder to understand than an imperative version. I think I just killed my own post here.

这篇关于为什么要“在...期间做"?在F#中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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