问题描述
我目前在理解Haskell的Either-Monad时遇到问题。 两者都是Monad数据类型的实例。因此,我可以像这样在Do块中使用Either ...
type MyAccount = Integer
transaction :: Integer -> MyAccount -> Either String MyAccount
transaction value account | value + account < 0 = Left "Error: insufficient funds!"
| otherwise = return $ account + value
Test1 :: Either String MyAccount
Test1 = do
account_state <- return 1000
r1 <- transaction (-200) account_state
r2 <- transaction (-1000) r1
return r2
这是一段有效的代码: account_state,r1和r2 的类型为Integer。因此,功能 transaction 可以正常工作。但是。
Test2 :: Either String MyAccount
Test2 = do
account_state <- return 1000
r1 <- transaction (-2000) account_state
r2 <- transaction (-1000) r1
return r2
在Test2中,我已将事务值从-200更改为-2000。因此, r1 应该为字符串“错误:资金不足!”。由于 transaction 不适用于字符串,因此应该存在类型错误。但是没有... Haskell在这里做什么?
交易(-1000)(“错误:资金不足!”)不起作用。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)