无法将预期类型“文本”与实际类型“[Char]”匹配

时间:2016-06-18 08:40:11

标签: haskell

这可能是一个非常愚蠢的问题,但我无法解决这个问题(因为我刚开始学习Haskell)。

我有一个简单的代码块:

module SomeTest where
import Data.Text

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

如果我用str打电话给我,我会:

<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"

我不知道为什么我会收到此错误以及如何通过它。 Text只是[Char]的同义词吗?

2 个答案:

答案 0 :(得分:26)

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

在您的情况下,添加{-# LANGUAGE OverloadedStrings #-}作为程序的第一行将使其编译。基本上你的字符串文字可以是Text类型。

答案 1 :(得分:0)

在我的情况下,我的代码正在执行String和Text值的串联:

 <div class="container">
  <div class="row">
      <div class="col-md-12">
          <div>
              <div class="form-group">
                  <label *ngIf="!fileData" (click)="fileInput.click()" class="custom-file-upload">
                      <i class="flaticon-attachment icon"></i> Attach Supporting File ...
                  </label>
                  <input #fileInput  type="file" (change)="onFileSelect($event.target.files[0])"/>
                  <label #fileInputLabel class="file-name-label">{{fileName}}</label>
                  <button #fileInputRemove *ngIf="fileData" type="button" class="btn btn-danger btn-send" (click)="onRemoveFile()">Remove Attachment</button>
              </div>
            </div>
      </div>
  </div>
</div>

"cd " ++ stackProjectLocation 属于文本类型,因为使用了"cd "

{-# LANGUAGE OverloadedStrings #-}是一个字符串

解决方案是使用stackProjectLocation

(<>) :: Semigroup a => a -> a -> a