9

Here's what I tried to do in ghci:

import Data.Text
strip "  abc  "

I get this error message:

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

I was expecting this to work because it was given on many web pages including this answer: In Haskell, how do you trim whitespace from the beginning and end of a string?

What am I doing wrong?

Community
  • 1
  • 1
Eric Normand
  • 3,806
  • 1
  • 22
  • 26

3 Answers3

16

You'll need to enable overloaded string literals in order to use string literals as Text values (otherwise string literals will always have the type String = [Char]).

Without overloaded string literals, you'll have to use pack to create a Text from a String, so:

strip $ pack "  abc  "
sepp2k
  • 363,768
  • 54
  • 674
  • 675
11

You should either start ghci using ghci -XOverloadedStrings or, if you are already in ghci and don't want to exit, set the flag dynamically using :set -XOverloadedStrings.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
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"]
perror
  • 7,071
  • 16
  • 58
  • 85
matrixmike
  • 31
  • 3
  • I will try and improve on my formatting - the above is an example of a working .hs file using ghc options as identified with {-# text #-} . – matrixmike Sep 30 '17 at 06:11