The recent GHC now has a MultiWayIf :
{-# LANGUAGE MultiWayIf #-} parseNumber :: String -> Maybe (String, String) parseNumber [] = Just ("", "") parseNumber (h:ls) | isDigit h = if | p == Nothing -> Just ([h], ls) | otherwise -> Just (h:fst d, snd d) | h == '.' = if | p == Nothing -> Nothing | not ('.' `elem` (snd d)) -> Just (h:(fst d), snd d) | otherwise = Nothing where p@ (~(Just d)) = parseNumber ls
But it is better written a little differently, without partiality.
{-# LANGUAGE MultiWayIf #-} parseNumber :: String -> Maybe (String, String) parseNumber [] = Just ("", "") parseNumber (h:ls) | isDigit h = if | Nothing <- p -> Just ([h], ls) -- PatternGuards, on by default | Just d <- p -> Just (h:fst d, snd d) | h == '.' = if | Nothing <- p -> Nothing | Just d <- p, not ('.' `elem` snd d) -> Just (h:(fst d), snd d) | otherwise = Nothing where p = parseNumber ls
and you can also use maybe .
parseNumber :: String -> Maybe (String, String) parseNumber "" = Just ("", "") parseNumber (h:hs) | isDigit h = maybe (Just ([h], hs)) (\(num, rest') -> Just (h:num, rest')) rest | h == '.' = maybe Nothing (\(num, rest') -> if '.' `elem` num then Nothing else Just (h:num, rest') ) rest -- This logic is a bit wonky; it doesn't really work | otherwise = Nothing where rest = parseNumber hs
HTNW
source share