Is it possible to nest guards in Haskell?

前端 未结 8 933
野的像风
野的像风 2020-12-10 16:52

Haskell newbie here, trying to write code to parse math expressions. Code:

isDigit :: Char -> Bool
isDigit c = c >= \'0\' && c <= \'9\'

pars         


        
8条回答
  •  不知归路
    2020-12-10 17:12

    No, but you can use cases if you'd like:

    parseNumber :: String -> Maybe (String, String)
    parseNumber [] = Just ("", "")
    parseNumber (h:ls)
        | isDigit h =
             case () of
               () | p == Nothing -> Just([h], ls)
                  | otherwise -> Just (h:fst d, snd d) -- Ends in a digit
        | h == '.' =
             case () of
               () | p == Nothing -> Nothing
                  | not ('.' `elem` (snd d)) -> Just (h:(fst d), snd d)
        | otherwise = Nothing
        where 
            p      = parseNumber ls
            Just d = parseNumber ls
    

    Alternatively, multiway if works in a similar manner (if True | p1 -> b ; | p2 -> c).

提交回复
热议问题