function - 如何使用带有 IO 读取 Int 的绑定重写“do”块?
问题描述
所以,我想prog
用>>
/>>=
绑定而不是do
and重写给定的函数<-
:
prog :: IO Int
prog =
do putStrLn "Hello there! How old are you?"
age <- (readLn :: IO Int)
let agedays = show $ age * 365
putStrLn $ "So you are at least than " ++ agedays ++ " days old."
return (read agedays)
重写更简单的函数对我来说不是问题,但是readLn :: IO Int
让我很头疼......
我的建议是:
prog :: IO Int
prog =
putStrLn "Hello there!How old are you?" >>
readLn::IO >>=
let agedays = \age -> show $ age * 365 >>
putStrLn $ "So you are at least than " ++ agedays ++ " days old."
但是,这不起作用,因为将 绑定readLn :: IO
到下一个匿名函数存在问题\age
。有什么帮助吗?
解决方案
您更改代码太多,例如从 中删除Int
,IO Int
并将 lambdas 插入错误的点。
像这样的东西应该工作:
prog =
putStrLn "Hello there! How old are you?" >>
(readLn :: IO Int) >>= \age ->
let agedays = show $ age * 365
in putStrLn $ "So you are at least than " ++ agedays ++ " days old." >>
return (read agedays)
推荐阅读
- symfony - 如何在 EasyAdmin 3 中添加自定义操作?
- android - 如何创建在 Android NDK 上运行的预构建库?
- python - Flask 无法在调试模式下重新启动,并且 Visual Studio 2019 中不支持颜色输出
- laravel - 在 Laravel 存储库模式中处理模型依赖项
- c++ - 不知道如何为我正在制作的课程增加价值
- java - 在 WSL 上安装 tomcat 9
- javascript - 仅当表单不为空时如何重定向,JavaScript 验证
- python - 使用 rowspan 在 tkinter 中画布未正确排列
- javascript - 使用 Laravel 和 Vue JS 上传多个文件
- node.js - Google Cloud Functions 错误:“找不到模块 'sharp'”,但它在我的 package.json 中