haskell - Couldn't match expected type ‘[Char]’ with actual type ‘Int’
问题描述
Working on a method to convert a number n into any base b and having some trouble.
Code:
int2Base :: Int -> Int -> String
int2Base n b
|n == 0 = "0"
|otherwise = (mod n b) ++ int2Base (div n b) b
and my error:
Couldn't match expected type ‘[Char]’ with actual type ‘Int’
In the second argument of ‘mod’, namely ‘b’
In the first argument of ‘(++)’, namely ‘(mod n b)’
It seems like a simply error but even when I cast it to a char it still expects '[Char]' not [Char]
解决方案
问题在这里:
(mod n b) ++ int2Base (div n b) b
“(mod nb)”产生一个Int,而不是一个String。
这应该解决它:
int2Base :: Int -> Int -> String
int2Base n b
|n == 0 = "0"
|otherwise = show(mod n b) ++ int2Base (div n b) b
推荐阅读
- c - Swift 中的 C 结构体
- javascript - 在猫鼬查询中更新深度嵌套的对象数组
- azure - 逻辑应用中不支持批准电子邮件流
- c++ - 可选值
- firebase - 在 androidmanifest.xml:73 中找不到资源“@xml/provider_paths”
- chart.js - 线和区域的chartjs条件格式
- node.js - 直接运行 Electron (electron app.js) 与从 package.json 脚本运行 (npm run electron)
- python - 使用opencv在python脚本中进行双重imshow
- javascript - 过滤带有卡片的 DIV
- c# - Entity Framework Core:使用 NOLOCK 读取和选择记录