如何在Haskell中正确使用toLower?

学生约会

我最近开始学习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>
Bartek Banachewicz

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

1)使用toLowerData.Char; 这是一个单一字符,您可以将其映射到字符串上:

map toLower "JhELlo"

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

unpack . toLower . pack $ "JhELlo"

实际上,还有其他版本toLower一个Data.Sequences似乎是多态的(因此应同时适用),但可能需要将mono-traversable软件包作为依赖项拉入

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章