简体   繁体   English

如何重用函数以在 Haskell 中有效地“循环”

[英]How to re-use a function to effectively 'loop' in Haskell

I've got a short interaction dialog where the computer asks for a number, and if it's smaller than 77, it states it's too small and to try again.我有一个简短的交互对话框,其中计算机要求输入一个数字,如果它小于 77,则表示它太小并重试。

The issue I have on hand is probably something simple, but as I'm relatively new I can't seem to get it too work, nor can find any related issues online.我手头的问题可能很简单,但由于我相对较新,我似乎无法解决它,也无法在网上找到任何相关问题。


import Text.Read

myread :: IO Int
myread  = do putStr "Please enter a number: "             
             x <- readLn             
             return x

isItLarge :: IO()
isItLarge = do
              result <- myread;
              if result < 77 then putStrLn "Tiny, try again!";
                                  myread
                             else putStrLn "Massive!";
               
               

In my logic, calling myread should revert it back to the top and allow you to continue until n > 77 is reached.在我的逻辑中,调用 myread 应该将其恢复到顶部并允许您继续直到达到 n > 77。 Any help would be appreciated:)任何帮助,将不胜感激:)

对于你的问题的具体方案,最简单的方法是只需更换你的第二呼叫myread用递归调用isItLarge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在子例程中,当调用包含循环的自写函数时,如何避免在循环内重复使用函数输入的复杂性? - In a sub, when calling a self-written function that includes a loop, how to avoid the complication of the re-use of function inputs within the loop? 如何获取JSON值并在许多函数中重复使用? - How Get JSON value and re-use it in many function? Flutter:如何提取 function 以便在多个屏幕中重复使用 - Flutter: How to extract a function for re-use in multiple screens 如何重用不同值的jQuery函数? - How to re-use jQuery function with different values? 如何重用相同的函数,但在同一测试脚本中从python中的不同位置调用它 - How to re-use the same function but call it from different locations in python in same test script 其他功能使用的Excel重用范围 - Excel re-use range that is used by another function 如何在功能上有效地使用元组? - How to use tuples effectively in function? for 循环被跳过 + 定义函数以重用字符串拆分 - for loops getting skipped + define function to re-use string splitting 正确格式化和重新使用代码块 - Correct formating and re-use of code blocks PHP中的Calendar Drop Down:保留选定的值并重新使用该函数 - Calendar Drop Down in PHP: keep selected value and re-use the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM