Miranda while 和 for 循环

发布于 2024-12-08 19:46:31 字数 152 浏览 0 评论 0原文

我正在寻找一种在 Miranda 中进行 while 循环或 for 循环的方法。

我正在尝试做类似的事情

while(blablanotfinished)
{
if(a=true)blabla
else blabla
}

I'm looking for a way to do while-loops or for-loops in Miranda.

I'm trying to do something like

while(blablanotfinished)
{
if(a=true)blabla
else blabla
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟稳重的好男人 2024-12-15 19:46:31

Miranda 没有 while 或 for 循环(无论如何,如果没有可变状态,这就没多大意义)。在大多数情况下,您可以改用高阶函数。如果没有高阶函数可以满足您的需要,您可以使用递归。

例如,如果您在命令式语言中有以下 while 循环:

f(start) {
    x = start
    while( !finished(x) ) {
        x = next(x)  
    }
    return x
}

您可以在 Miranda 中递归地表达它,如下所示:

f x = if finished x then x else f (next x)

Miranda doesn't have while- or for-loops (which wouldn't make much sense without mutable state anyway). In most cases you can use higher order functions instead. In cases where there is no higher-order function which does what you need, you can use recursion.

For example if you have the following while-loop in an imperative language:

f(start) {
    x = start
    while( !finished(x) ) {
        x = next(x)  
    }
    return x
}

You would express it recursively in Miranda like this:

f x = if finished x then x else f (next x)
温柔一刀 2024-12-15 19:46:31

函数式编程风格中的 while/repeat/for 循环如下所示:


while :: (*->bool) -> (*->*) -> * -> *
while p ff state
  = state                , if ~ (p state)
  = while p ff (ff state), otherwise

示例:将数字添加到其相反的位置,直到它成为回文。
提示:起始值 196 导致非常大的数字。


isPalindrome :: num -> bool
isPalindrome n = (str = reverse str) where str = shownum n

addPalindrome :: num -> num
addPalindrome n = n + rev where rev = (numval.reverse.shownum) n

test196 :: num -> num
test196 n = while ((~).isPalindrome) addPalindrome n

test = test196 89

我希望,有人仍然对戈弗/米兰达/哈斯克尔感兴趣。

安娜玛丽·佩森(德国)

while/repeat/for-loops in functional programming style look like this:


while :: (*->bool) -> (*->*) -> * -> *
while p ff state
  = state                , if ~ (p state)
  = while p ff (ff state), otherwise

Sample: Add number to its reverse until it is a palindrome.
Hints: Start value 196 leads to VERY big numbers.


isPalindrome :: num -> bool
isPalindrome n = (str = reverse str) where str = shownum n

addPalindrome :: num -> num
addPalindrome n = n + rev where rev = (numval.reverse.shownum) n

test196 :: num -> num
test196 n = while ((~).isPalindrome) addPalindrome n

test = test196 89

I hope, somebody is still interested in Gofer/Miranda/Haskell.

Annemarie Paysen (Germany)

四叶草在未来唯美盛开 2024-12-15 19:46:31

在 Miranda 中(一般来说,在纯函数式编程语言中)不鼓励使用 WHILE、FOR 等循环结构。您应该通过递归进行迭代。

In Miranda (and in general, in purely functional programming languages) the use of looping constructs like WHILE, FOR, etc. is discouraged. You're expected to do iteration via recursion.

遗心遗梦遗幸福 2024-12-15 19:46:31

与许多其他函数式语言一样,Miranda 没有 for 或 while 循环。相反,您可以使用递归来编写循环,列表推导式< /a> 或 更高阶功能

Like many other functional languages, Miranda does not have for- or while-loops. Instead, you write loops using recursion, list comprehensions or higher-order functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文