gpt4 book ai didi

haskell - 在 Haskell 中使用 isUpper、isLower 和 isDigit

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

我在如何使用 isUpper、isLower 和 isDigit 时遇到了问题。具体来说,我试图获取一个字符串并为字符串中的每个字符返回一个元组列表,其中包含三个 Bool 值,用于表示字符是大写字母、小写字母还是数字。所以字符串“Ab2”将返回列表[(True, False, False), (False, True, False), (False, False, True)]。这就是我所拥有的:

import Data.Char
uppercaseList :: [a] -> [(Bool, Bool, Bool)]
uppercaseList xs = [(isUpper, isLower, isDigit)]

我想我需要将字符串的字符传递给 isUpper、isLower 和 isDigit,但我不知道该怎么做。如果这是一个愚蠢的问题,我很抱歉,但到目前为止我没有发现任何东西可以解决我的困惑。

最佳答案

您需要检查列表中的每个元素( xs )。通常这种任务是通过使用 map 来完成的。

import Data.Char
uppercaseList :: String -> [(Bool, Bool, Bool)]
uppercaseList xs = map (\x -> (isUpper x, isLower x, isDigit x)) xs

或列表理解
uppercaseList xs = [ (isUpper x, isLower x, isDigit x) | x <- xs ]

或者从头开始写
uppercaseList [] = []
uppercaseList (x:xs) = (isUpper x, isLower x, isDigit x) : uppercaseList xs

关于haskell - 在 Haskell 中使用 isUpper、isLower 和 isDigit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211868/

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