Haskell의 유효성 검사기 기능

4012 단어 haskellprogramming
{-# LANGUAGE TypeApplications #-}

module Main where

import Data.Char (isAlphaNum, isSpace)

maxLength :: String -> Maybe String
maxLength "" = Nothing
maxLength xs =
  case (length xs > 20) of
    True -> Nothing
    False -> Just xs

allAlpha :: String -> Maybe String
allAlpha "" = Nothing
allAlpha xs =
  case (all isAlphaNum xs) of
    False -> Nothing
    True -> Just xs

stripSpace :: String -> Maybe String
stripSpace "" = Nothing
stripSpace (x : xs) =
  case (isSpace x) of
    True -> stripSpace xs
    False -> Just (x : xs)

validatePassword :: String -> Maybe String
validatePassword password =
  stripSpace password >>= allAlpha >>= maxLength

main :: IO ()
main = do
  putStrLn "Please enter a password"
  password <- getLine
  print (validatePassword password)

좋은 웹페이지 즐겨찾기