How to filter a Maybe value

Viewed 2559

I am trying to create a function that validates an input String -> Maybe Int. I check to see if the input string is a number then check if that number is in a range. So far I have

validateNumber :: String -> Maybe Int
validateNumber n  = go $ (readMaybe::String -> Maybe Int) n
  where
    go (Just a) = inRange a
    go Nothing  = Nothing

inRange :: Int -> Maybe Int
inRange n
  | n > 0     = Just n
  | otherwise = Nothing

This feels like poor code. How should this be written?

Also, if I am trying to loop a function if it returns Nothing, what is the best way to do so:

So to loop the main function, I am doing :

case v of
  Nothing -> main
  Just x  -> {do something}
3 Answers
Related