0

所以,我想prog>>/>>=绑定而不是doand重写给定的函数<-

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。有什么帮助吗?

4

2 回答 2

6

您更改代码太多,例如从 中删除IntIO 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)
于 2019-02-26T14:06:30.327 回答
5

您可以让类型推断为您完成工作,

prog :: IO Int
prog =
     putStrLn "Hello there! How old are you?" >>
     readLn >>= (\ age ->
     let agedays = age * 365 in
       putStrLn ("So you are at least " ++ show agedays ++ " days old.") >>
       return agedays )

由于您已经指定prog :: IO Int,它意味着return agedays :: IO Intagedays :: Int

然后, in 的两个操作数*必须age * 365是相同的类型,特别是 的agedays,因为我们agedays = age * 365有。因此它age :: Int已经遵循了。

于 2019-02-26T14:13:34.423 回答