Validator Function in Haskell

{-# 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

al…


This content originally appeared on DEV Community and was authored by Vivek Raj

{-# 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)


This content originally appeared on DEV Community and was authored by Vivek Raj


Print Share Comment Cite Upload Translate Updates
APA

Vivek Raj | Sciencx (2022-07-12T20:16:32+00:00) Validator Function in Haskell. Retrieved from https://www.scien.cx/2022/07/12/validator-function-in-haskell/

MLA
" » Validator Function in Haskell." Vivek Raj | Sciencx - Tuesday July 12, 2022, https://www.scien.cx/2022/07/12/validator-function-in-haskell/
HARVARD
Vivek Raj | Sciencx Tuesday July 12, 2022 » Validator Function in Haskell., viewed ,<https://www.scien.cx/2022/07/12/validator-function-in-haskell/>
VANCOUVER
Vivek Raj | Sciencx - » Validator Function in Haskell. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/12/validator-function-in-haskell/
CHICAGO
" » Validator Function in Haskell." Vivek Raj | Sciencx - Accessed . https://www.scien.cx/2022/07/12/validator-function-in-haskell/
IEEE
" » Validator Function in Haskell." Vivek Raj | Sciencx [Online]. Available: https://www.scien.cx/2022/07/12/validator-function-in-haskell/. [Accessed: ]
rf:citation
» Validator Function in Haskell | Vivek Raj | Sciencx | https://www.scien.cx/2022/07/12/validator-function-in-haskell/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.