Haskell无法与实际类型'Char'匹配预期类型'String' [英] Haskell Couldn't match expected type 'String' with actual type 'Char'

查看:312
本文介绍了Haskell无法与实际类型'Char'匹配预期类型'String'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我得到这个错误。这是一个任务,我要从一个整数转换为十六进制值。当我通过 16 修改整数值时,我调用了这个帮助器转换函数。 (与递归调用中除以 16 的整数值连接)



这里是我的代码:

  changeToHex :: Integer  - >字符串
- 主函数

toHex :: Integer - >字符串
toHex x
| x == 0 ='0'
| x == 1 ='1'
| x == 2 ='2'
| x == 3 ='3'
| x == 4 ='4'
| x == 5 ='5'
| x == 6 ='6'
| x == 7 ='7'
| x == 8 ='8'
| x == 9 ='9'
| x == 10 ='A'
| x == 11 ='B'
| x == 12 ='C'
| x == 13 ='D'
| x == 14 =' E'
| x == 15 ='F'


解决方案

使用单引号('F')为您提供 Char 字面值。对于字符串,它实际上是 Char 值的列表,您应该使用双引号(<$ c由于字符串

的别名,所以

Char 转换为字符串,请使用code> [Char] $ c>,你只能将 Char 包装在一个元素列表中。这样做的函数可能如下所示:

  stringFromChar :: Char  - >字符串
stringFromChar x = [x]

这通常是内联编写的,如(:[]),相当于 \x - > (x:[]) \x - >另外,您可以使用 Enum 来显着简化代码。 typeclass:

  toHexDigit :: Int  - > Char 
到HexDigit x
| x < 0 =错误toHex:负数字值
| x < 10 = toEnum $ fromEnum'0'+ x
| x < 15 = toEnum $ fromEnum'A'+ x - 10
|否则=错误toHex:数字值太大

更一般地说,任何时候你有一个像:

  fx 
| x == A = ...
| x == B = ...
| x == C = ...
...

您可以将其转换为 case

  fx = case x 
A - > ...
B - > ...
C - > ...
...


I am wondering why I'm getting this error. It's for an assignment where i'm to convert from an integer to a hex value. I call this helper conversion function when I mod the integer value by 16. (concatenated with the integer value which I then divide by 16 in a recursive call)

Here is my code:

    changeToHex :: Integer -> String
    --main function

    toHex :: Integer -> String
    toHex x
        |x == 0         = '0'
        |x == 1         = '1'
        |x == 2         = '2'
        |x == 3         = '3'
        |x == 4         = '4'
        |x == 5         = '5'
        |x == 6         = '6'
        |x == 7         = '7'
        |x == 8         = '8'
        |x == 9         = '9'
        |x == 10        = 'A'
        |x == 11        = 'B'
        |x == 12        = 'C'
        |x == 13        = 'D'
        |x == 14        = 'E'
        |x == 15        = 'F'

解决方案

Using single quotes ('F') gives you a Char literal. For a String literal, which is in fact a list of Char values, you should use double quotes ("F").

Since String is an alias for [Char], if you want to convert from a Char to a String, you can merely wrap the Char in a one-element list. A function to do so might look like:

stringFromChar :: Char -> String
stringFromChar x = [x]

This is typically written inline, as (:[]), equivalent to \x -> (x : []) or \x -> [x].

As an aside, you can simplify your code considerably, using for example the Enum typeclass:

toHexDigit :: Int -> Char
toHexDigit x
  | x < 0 = error "toHex: negative digit value"
  | x < 10 = toEnum $ fromEnum '0' + x
  | x < 15 = toEnum $ fromEnum 'A' + x - 10
  | otherwise = error "toHex: digit value too large"

More generally, any time you have a function like:

f x
  | x == A = ...
  | x == B = ...
  | x == C = ...
  ...

You can convert that to a less repetitious, more efficient equivalent with case:

f x = case x of
  A -> ...
  B -> ...
  C -> ...
  ...

这篇关于Haskell无法与实际类型'Char'匹配预期类型'String'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆