2011-06-15 112 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