gpt4 book ai didi

haskell - 在 Haskell 中制作小写字符串列表

转载 作者:行者123 更新时间:2023-12-04 14:46:48 25 4
gpt4 key购买 nike

我有一个字符串列表 ["Hi", "hELLO", "nO"]我想将所有内容都转换为小写。我为此写了一个函数:

import Data.Char(toLower)
makeSmall xs = map toLower xs
它可以编译,但是当我这样做时 makeSmall ["Hi", "hELLO", "nO"]它给了我这个错误
<interactive>:2:12: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the expression: "Hi"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]

<interactive>:2:17: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the expression: "hELLO"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]

<interactive>:2:25: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the expression: "nO"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]
我试图了解如何使我的函数适用于字符串列表,而不仅仅是字符串

最佳答案

toLower :: Char -> Char 映射一个 CharChar ,这意味着 map toLower会走单String ,它将返回一个 String .
如果要处理String 的列表s,你应该使用第二个 map :

makeSmall :: [String] -> [String]
makeSmall xs = map (map toLower) xs
或更短:
makeSmall :: [String] -> [String]
makeSmall = map (map toLower)
因此,我们制作了一个使用 map toLower 的映射器。作为 map 功能。因此,此功能将适用于单个 String .由于我们为此制作了一个映射器,因此我们处理了 String 的列表。 s。

关于haskell - 在 Haskell 中制作小写字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69876975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com