首页 > 解决方案 > 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]

标签: haskell

解决方案


问题在这里:

(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

推荐阅读