Haskell 中 read 函数的大小写匹配
我想知道如何检查Haskell中的读取功能的成功或失败(以PRELUDE.NO READ。在我的情况下,我运行”(读取格式化:: int)”,以代码格式化记录结构,其中字段可能是字符串形式的单个int,但也可能包含其他内容。我只想将功能仅应用于读取返回int的字段。谢谢。
I am wondering how to check success or failure (resulting in Prelude.read: no parse) of the read function in Haskell. In my case i run "(read formatted :: Int)" in code formatting a record structure, where the fields might be a single Int in String form but might also contain something else. I want to apply my function only to the fields where the read returns an Int. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该考虑 Text.Read 中的
readMaybe
。一旦在Maybe
monad 中返回值,您就可以使用case
来决定要做什么。如果你想更冒险一点,既然数据在 Maybe monad 中,我们可以将 Maybe 视为函子并使用应用函子来处理它们。
You should consider
readMaybe
from Text.Read. Once the value is returned within theMaybe
monad then you can usecase
to decide what to do.If you want to be more adventurous, now that the data is in the Maybe monad, we can treat the Maybe as a functor and use applicative functors to process them.