Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly use toLower in Haskell?

Tags:

haskell

I've recently started to learn Haskell and wanted to convert something to lowercase. I looked up the function "toLower", but it doesn't seem to work.

Prelude> import Data.Text
Prelude Data.Text> toLower "JhELlo"

<interactive>:2:9: error:
    * Couldn't match expected type `Text' with actual type `[Char]'
    * In the first argument of `toLower', namely `"JhELlo"'
      In the expression: toLower "JhELlo"
      In an equation for `it': it = toLower "JhELlo"
Prelude Data.Text> toLower 'JhELlo'

<interactive>:3:9: error:
    * Syntax error on 'JhELlo'
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    * In the Template Haskell quotation 'JhELlo'
Prelude Data.Text>
like image 809
schuelermine Avatar asked Oct 04 '17 15:10

schuelermine


1 Answers

It doesn't work, because the version you tried to use operates on Text, and not String. Those are two distinct types. You have two options at this point:

1) Use toLower from Data.Char; this one operates on a single character, and you can map it over your string:

map toLower "JhELlo"

2) Convert you string to Data.Text (and optionally back again):

unpack . toLower . pack $ "JhELlo"

There are actually other versions of toLower around; the one in Data.Sequences seems to be polymorphic (so should work on both), but it might require pulling in the mono-traversable package as a dependency.

like image 55
Bartek Banachewicz Avatar answered Sep 28 '22 11:09

Bartek Banachewicz