2011-06-15 71 views
8

這是我試圖在ghci中要做到:爲什麼Data.Text示例不適合我?

import Data.Text 
strip " abc " 

我收到此錯誤信息:

<interactive>:1:6: 
    Couldn't match expected type `Text' against inferred type `[Char]' 
    In the first argument of `strip', namely `" abc "' 
    In the expression: strip " abc " 
    In the definition of `it': it = strip " abc " 

我期待這個工作,因爲它是在許多網頁包括這個答案給出:In Haskell, how do you trim whitespace from the beginning and end of a string?

我在做什麼錯?

回答

15

您需要啓用overloaded string literals才能使用字符串文字作爲Text值(否則字符串文字將始終爲String = [Char])。

沒有重載字符串文字,你將不得不使用packString創建Text,所以:

strip $ pack " abc " 
+5

重載字符串是最容易通過將編譯到您的名爲.hs文件的第一行來啓用:'{ - #語言OverloadedStrings# - }' – 2011-06-16 21:09:31

10

您應該使用ghci -XOverloadedStrings ghci的開始,或者,如果你已經在ghci中和唐不想退出,請使用:set -XOverloadedStrings動態設置國旗。

0
{-# OPTIONS_GHC -fwarn-missing-signatures #-} 
{-# LANGUAGE OverloadedStrings #-} 

import Data.Text as MJ 

main :: IO() 
main = do 

     print $ strip $ pack " abc " 
     print $ MJ.intercalate "as" ["1","2","3"] 
+0

我會盡量提高自己的格式 - 上面是一個以{ - #text# - }標識的使用ghc選項的.hs文件的示例。 – matrixmike 2017-09-30 06:11:05