简体   繁体   English

计算小写和大写,然后将其作为元组返回

[英]Count lowercase and uppercase then return it as a tuple

I want to implement a function called countLowerUpper which takes a String and count lower and uppercases and return it as a tuple.我想实现一个名为 countLowerUpper 的函数,它接受一个字符串并计算小写和大写并将其作为元组返回。 For example : countLowerUpper "TeST" should return (1,3) .例如: countLowerUpper "TeST"应该返回(1,3) I don't know how to return a tuple properly.我不知道如何正确返回元组。 I am forced to use countLowerUpper :: String-> (Int,Int) .我被迫使用countLowerUpper :: String-> (Int,Int) I can't change it.我无法改变它。

import Data.Char
countLowerUpper :: String-> (Int,Int)
countLowerUpper = (length . filter (isLower),length . filter(isUpper))

EDIT After received help I change my code to:编辑收到帮助后,我将代码更改为:

import Data.Char

countLowerUpper :: String-> (Int,Int)
countLowerUpper str  = (lowerCount,upperCount)
    where lowerCount =length . filter(isLower)
          upperCount =length . filter(isUpper)

But still have got error:但仍然有错误:

 error:
    • Couldn't match expected type ‘Int’
                  with actual type ‘[Char] -> Int’
    • Probable cause: ‘upperCount’ is applied to too few arguments
      In the expression: upperCount
      In the expression: (lowerCount, upperCount)
      In an equation for ‘countLowerUpper’:
          countLowerUpper str
            = (lowerCount, upperCount)
            where
                lowerCount = length . filter (isLower)
                upperCount = length . filter (isUpper)
  |
4 | countLowerUpper str  = (lowerCount,upperCount)   | 

I know that isLower don't have got any argument but I don't know what to use.我知道 isLower 没有任何论据,但我不知道该使用什么。 Maybe str?也许str? Or use head and tail of string?或者使用字符串的头部和尾部?

import Data.Char

countLowerUpper :: String -> (Int,Int)
countLowerUpper str = (lowerCount,upperCount)
    where lowerCount = undefined
          upperCount = undefined

You can do this.你可以这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM