Home > Enterprise >  Calling multiple functions
Calling multiple functions

Time:02-03

I've been trying to call other functions into my main function which will return "valid" if the boolean value is True. The problem is that one of my functions returns a false value and since the others are True, am not receiving the expected output.

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

The localPart is a function that I will apply on my main, which will validate if the boolean values from each function are True. The charact will validate if the strings have any type of special characters and filter it to not return True if the String has a special character.

For the repeatedLocal it should check if the specific values repeat more than 1 time in a string - This is part of the code where the value instead of returning True, the function return False if the string doesn't have the specific value which is '.' and '_' instead return True if the characters repeat in the string.

the lowerCase is to return True if the String is lowercase and num if there is a number in the string

The expected output should be, if the charact && lowerCase && num are True then the String is valid and if repeatedLocal is True the string is invalid

CodePudding user response:

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"

I think numbers and characters are not recognized as lower case, so I add a function which eliminates nums and specific caracters (rmNumCh) and use this in the 'lowerCase' function.
And I also think you shouldn't use recursion for the 'localPart' function, because all functions returning boolean values check for a whole string.

  • Related