[英]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.