![](/img/trans.png)
[英]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?
[英]How to re-use a function to effectively 'loop' in Haskell
我有一个简短的交互对话框,其中计算机要求输入一个数字,如果它小于 77,则表示它太小并重试。
我手头的问题可能很简单,但由于我相对较新,我似乎无法解决它,也无法在网上找到任何相关问题。
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!";
在我的逻辑中,调用 myread 应该将其恢复到顶部并允许您继续直到达到 n > 77。 任何帮助,将不胜感激:)
对于你的问题的具体方案,最简单的方法是只需更换你的第二呼叫myread
用递归调用isItLarge
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.