haskell 调用多个函数

svdrlsy4  于 2023-02-04  发布在  其他
关注(0)|答案(2)|浏览(165)

我一直在尝试调用其他函数到我的主函数中,如果布尔值为True,它将返回“valid”。问题是我的一个函数返回了一个false值,由于其他函数都为True,所以我没有收到预期的输出。

import Data.Char(isLower)

charact :: String -> Bool
charact = not . any (`elem` "! #$%&'*+-/><=?^`{|}:,``[]];")

repeatedLocal :: [Char] -> Bool
repeatedLocal [] = False
repeatedLocal (x:xs) = if elem x ['.','_'] then elem x xs else  repeatedLocal xs

lowerCase :: [Char] -> Bool
lowerCase x = if all isLower x then True else False

num :: String -> Bool
num = any (`elem` "0123456789")

localPart ::[Char] -> String
localPart [] = "E-mail Invalido"
localPart (x:xs) | length (x:xs) <= 24 && charact (x:xs) && lowerCase (x:xs) && num (x:xs) == True = "E-mail Valido"
                 | repeatedLocal (x:xs) == False = "E-mail Invalido"
                 | otherwise = localPart xs

localPart是一个函数,我将在我的main上应用它,它将验证每个函数的布尔值是否为True,charact将验证字符串是否包含任何类型的特殊字符,如果String包含特殊字符,则过滤它以不返回True。
对于repeatedLocal,应检查特定值是否在字符串中重复超过1次-这是代码的一部分,其中,如果字符串没有特定值(即“.”),则函数返回False,而不是返回True,如果字符串中的字符重复,则函数返回True。
如果字符串为小写,则lowerCase返回True;如果字符串中包含数字,则num返回True
预期输出应为:如果charactlowerCasenum为True,则字符串有效;如果repeatedLocal为True,则字符串无效

9rbhqvlz

9rbhqvlz1#

import Data.Char(isLower)

charact :: String -> Bool
charact = not . any (`elem` "! #$%&'*+-/><=?^`{|}:,``[]];")

repeatedLocal :: [Char] -> Bool
repeatedLocal [] = False
repeatedLocal (x:xs) = if elem x ['.','_'] then elem x xs else  repeatedLocal xs

lowerCase :: [Char] -> Bool
lowerCase x = if all isLower (rmNumCh x) then True else False

num :: String -> Bool
num = any (`elem` "0123456789")

rmNumCh :: String -> String
rmNumCh [] = []
rmNumCh (x:xs) = if (x `elem` "0123456789._") then rmNumCh xs 
                                              else x:rmNumCh xs

localPart :: String -> String
localPart str
  | length str <= 24 && charact str && lowerCase str 
                     && num str && not (repeatedLocal str) = "E-mail Valido"
  | otherwise = "E-mail Invalido"

我认为数字和字符不被识别为小写,所以我添加了一个函数,消除了nums和特定的字符(rmNumCh),并在'lowerCase'函数中使用它。
我还认为不应该对'localPart'函数使用递归,因为所有返回布尔值的函数都检查整个字符串。

oalqel3c

oalqel3c2#

这是怎么回事?

import Data.Char(isLower)

charact :: String -> Bool
charact = not . any (`elem` "! #$%&'*+-/><=?^`{|}:,``[]];")

repeatedLocal :: String -> Bool
repeatedLocal [] = False
repeatedLocal (x:xs) = if elem x ['.','_'] then elem x xs else  repeatedLocal xs

lowerCase :: String -> Bool
lowerCase x = all isLower (rmNumCh x)

num :: String -> Bool
num = any (`elem` "0123456789")

rmNumCh :: String -> String
rmNumCh [] = []
rmNumCh (x:xs) = if x `elem` "0123456789._" then rmNumCh xs else x:rmNumCh xs

localPart :: String -> String
localPart str
  | foldl (\acc f -> f str && acc) True [(<=24).length,charact,lowerCase,num,not.repeatedLocal] = "E-mail Valido"
  | otherwise = "E-mail Invalido"

我使用"foldl"函数。

相关问题