Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the Data.Text examples work for me?

Tags:

haskell

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?

like image 766
Eric Normand Avatar asked Jun 15 '11 20:06

Eric Normand


2 Answers

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  "
like image 184
sepp2k Avatar answered Sep 30 '22 12:09

sepp2k


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.

like image 33
Daniel Wagner Avatar answered Sep 30 '22 11:09

Daniel Wagner