Created
November 29, 2019 15:52
-
-
Save SergeyStretovich/81cbd420692b34fded84525dd3648833 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import Data.Maybe | |
import Data.Char | |
data Person = Person { name :: String, age :: Int } deriving (Show) | |
main :: IO () | |
main = do | |
let pList = [Person "Jack" 11,Person "Bill" 48,Person "Tom" 19,Person "Dane9" 22,Person "Jimmy9" 13] | |
let validatedL = map validatePerson pList | |
mapM_ (\x -> do | |
case x of | |
Right v -> putStrLn $ show v | |
Left (z,y)-> putStrLn $ (show z) ++ " " ++ (show y) | |
) validatedL | |
let filteredL = catEither validatedL | |
putStrLn "============================" | |
mapM_ (\x-> putStrLn (show x) ) filteredL | |
validateAge::Person -> Maybe String | |
validateAge p | |
| (age p) > 18 = Nothing | |
| otherwise = Just "Age must be over 18" | |
validateName::Person -> Maybe String | |
validateName p | |
| (any isDigit (name p)) == False = Nothing | |
| otherwise = Just "Name contains invalid characters" | |
validatePerson :: Person -> Either (Person,[String]) Person | |
validatePerson person = if len == 0 then Right person else Left (person, catMaybes listErr) | |
where | |
listErr = foldl (\x func -> [func person]++ x ) [] [validateAge,validateName] | |
len = length $ catMaybes listErr | |
catEither :: [Either a b] -> [b] | |
catEither ls = [x | Right x <- ls] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment