On Oct 10, 2008, at 5:46 PM, Jonathan Cast wrote:


  identifier = lexeme $ match "[[:lower:]_][[:alphanum:]_]*"

(pretending match :: String -> Parser String is a regex engine).

vs.

  identified = lexeme $ do
    c <- satisfy isLower <|> satisfy (=='_')
    s <- many $ satisfy isAlphaNum <|> satisfy (=='_')
    return (c:s)

lexeme $ (:) <$> (lowerChar <|> char '_') <*> (many $ alphaNum <|> char '_') ?

or (since we're not really talking about full fledged parsers that need lexemes here or such, but usually interpreting a single string, otherwise regexes will quickly become atrocious)

foo (x:xs) | isLower x || x == '_', (xs', rest) <- break alphaOrUnder xs = Just (x : xs', rest)
                  | otherwise = Nothing
  where alphaOrUnder = liftM2 (||) isAlphaNum (=='_')
foo [] = Nothing

A bit more verbose, sure, but operating on text functionally makes it really easy to reason about what your parser is actually doing, unlike the mysteries of a regex.

--Sterl.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to