Haskell中while循环的等效语句是什么? [英] What is the equivalent statement of a while loop in Haskell?
问题描述
对于Haskell来说,我很陌生,我想知道如何1)计算某物直到满足特定条件,然后2)返回计算出的值.
Being very new to Haskell, I'm wondering how to 1) compute something until a certain criterion is satisfied, and then 2) return the computed value.
在我所知道的语言中,您将为此使用while循环.您如何在Haskell中做到这一点?
In the languages I know, you would use a while loop for that. How do you do it in Haskell?
推荐答案
您应使用递归:
func :: <function type>
func <arguments> =
if condition
then <recursive call>
else computedValue
将来还会发现其他实用程序,例如 map 或 fold
-family函数之一.
There are also other utilities you'll discover in the future, such as until
, that will help you with this. In the end it really depends on the semantic of the loop and condition. For example if the condition is simply "until we reach the end of a list" you can simply use map
or one of the fold
-family functions.
这篇关于Haskell中while循环的等效语句是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!