如何在Haskell中正确使用toLower?

时间:2017-10-04 15:29:13

标签: haskell

我最近开始学习Haskell并希望将某些内容转换为小写。我查找了函数“toLower”,但它似乎没有用。

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>

1 个答案:

答案 0 :(得分:4)

它不起作用,因为您尝试使用的版本在Text上运行,而不是String。这是两种截然不同的类型。此时您有两种选择:

1)使用toLower中的Data.Char;这个操作一个字符,你可以将它映射到你的字符串:

map toLower "JhELlo"

2)将您的字符串转换为Data.Text(并可选择再返回):

unpack . toLower . pack $ "JhELlo"

实际上other versions of toLower左右; Data.Sequences中的那个似乎是多态的(因此应该同时使用),但它可能需要将mono-traversable包作为依赖项。