haskell - 将数字字符串转换为整数
问题描述
我正在尝试在 Haskell 中编写这个名为 atoi 的函数,它采用一串表示整数的数字到整数本身。
例如,atoi "123"
应该给出 123。
到目前为止,这是我的实现:
atoi :: String -> Int
atoi str = show str :: Int
我收到一个错误说明
无法匹配类型
解决方案
使用read
:
atoi :: String -> Int
atoi s = read s :: Int
例子:
Prelude> atoi s = read s :: Int
Prelude> atoi "12345"
12345
推荐阅读
- php - Laravel 5.8 StreamDownload 文件附件重定向而不是下载
- github - 为标签重新运行 Github 操作更新
- solr - 希望改进多值字段上的 Solr 过滤器查询,这需要超过 2 秒
- javascript - 用另一个 Jquery 函数触发一个 Jquery 函数
- c# - dotnet 测试例程失败
- asp.net - 在 .net 核心中,如何在 Web 根目录下下载具有特殊扩展名的静态文件?
- azure-devops - 连接到 Azure DevOps 服务时,如何在 Visual Studio 而不是 Web 浏览器中打开 Bug/PBI?
- python-3.x - 请解释以下代码的工作原理和下面提到的一些问题
- python - QTreeView QAbstractItemModel 父级在删除项目后崩溃,有时崩溃
- parsing - 在确定标记之前让词法分析器考虑解析器?