无法与实际类型为“[Char]”的预期类型“文本”匹配 [英] Couldn't match expected type `Text' with actual type `[Char]'

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

问题描述

这可能是一个非常不好的问题,但我无法克服这个问题(正如我刚开始学习Haskell时所说的那样)。

我有一个简单的块代码:

 模块SomeTest其中
导入Data.Text

str =替换ofo barofofo

如果我用 str ,我得到:


 < interactive>:108:19:error :
*无法与实际类型`[Char]'相匹配的期望类型'Text'
*在'​​Data.Text.replace'的第一个参数中,即``ofo''
在表达式中:Data.Text.replaceofobarofofo
在'it'的等式中:it = Data.Text.replaceofobarofofo

< interactive>:108:25:error:
*无法与实际类型为[[Char]'的$ Text'匹配'
*在Data的第二个参数.Text.replace',即``bar''
在表达式中:Data.Text.replaceofobarofofo
在a 'it'的等式:it = Data.Text.replaceofobarofofo

< interactive>:108:31:error:
* Could not在'Data.Text.replace'的第三个参数中,即``ofofo''
在预期类型'Text'中匹配实际类型`[Char]'
在表达式:Data.Text中。替换ofobarofofo
在it的等式中:it = Data.Text.replaceofobarofofo


我不知道为什么我得到这个错误以及如何通过它。不是文本只是 [Char]

的同义词

解决方案

不幸的是,Haskell对于字符串有几种冲突的类型。字符串文字通常是 String 类型,它只是 [Char] 的别名。因为这是字符串的低效表示,所以还有其他选择,比如 Text



在这种情况下, code> { - #LANGUAGE OverloadedStrings# - } 作为程序的第一行将会使其编译。基本上你的字符串文字可以是 Text 然后。


This might be a really noobish question, but I can't get past this problem (as I just started learning Haskell).

I have a simple block of code:

module SomeTest where
import Data.Text

str =  replace "ofo" "bar" "ofofo"

If I call this with str, I get:

<interactive>:108:19: error:
* Couldn't match expected type `Text' with actual type `[Char]'
* In the first argument of `Data.Text.replace', namely `"ofo"'
  In the expression: Data.Text.replace "ofo" "bar" "ofofo"
  In an equation for `it': it = Data.Text.replace "ofo" "bar" "ofofo"

<interactive>:108:25: error:
* Couldn't match expected type `Text' with actual type `[Char]'
* In the second argument of `Data.Text.replace', namely `"bar"'
  In the expression: Data.Text.replace "ofo" "bar" "ofofo"
  In an equation for `it': it = Data.Text.replace "ofo" "bar" "ofofo"

<interactive>:108:31: error:
* Couldn't match expected type `Text' with actual type `[Char]'
* In the third argument of `Data.Text.replace', namely `"ofofo"'
  In the expression: Data.Text.replace "ofo" "bar" "ofofo"
  In an equation for `it': it = Data.Text.replace "ofo" "bar" "ofofo"

I don't know why I'm getting this error and how to get pass it. Isn't Text just a synonym for [Char]?

解决方案

Unfortunately, Haskell has several conflicting types for strings of characters. String literals are usually of type String which is just an alias for [Char]. Because that is an inefficient representation of strings, there are alternatives, like Text.

In your case, adding {-# LANGUAGE OverloadedStrings #-} as the first line of your program will make it compile. Basically your string literals can be of type Text then.

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

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