diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-12-11 10:28:11 +0100 |
| commit | 98329e0a3dd4f78b5d815ac3896272ec70904901 (patch) | |
| tree | 80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/24/ParserExercises/src/Base10Integer.hs | |
| parent | 3624c712d72d246f21d4e710cec7c11e052e0326 (diff) | |
| download | book-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz | |
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/24/ParserExercises/src/Base10Integer.hs')
| -rw-r--r-- | Haskell-book/24/ParserExercises/src/Base10Integer.hs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Haskell-book/24/ParserExercises/src/Base10Integer.hs b/Haskell-book/24/ParserExercises/src/Base10Integer.hs new file mode 100644 index 0000000..4e8ee3a --- /dev/null +++ b/Haskell-book/24/ParserExercises/src/Base10Integer.hs @@ -0,0 +1,52 @@ +module Base10Integer where + +import Control.Applicative +import Text.Trifecta + +-- 2. Write a parser for positive integer values. Don't reuse the preexisting +-- digit or integer functions, but you can use the rest of the libraries we've +-- shown you so far. You are not expected to write a parsing library from +-- scratch. +-- +-- Hint: Assume you're parsing base-10 numbers. Use arithmetic as a cheap +-- "accumulator" for your final number as you parse each digit left-to-right. + +parseDigit :: Parser Char +parseDigit = (char '0') + <|> (char '1') + <|> (char '2') + <|> (char '3') + <|> (char '4') + <|> (char '5') + <|> (char '6') + <|> (char '7') + <|> (char '8') + <|> (char '9') + +charToDigit :: Char -> Integer +charToDigit c = case c of + '0' -> 0 + '1' -> 1 + '2' -> 2 + '3' -> 3 + '4' -> 4 + '5' -> 5 + '6' -> 6 + '7' -> 7 + '8' -> 8 + '9' -> 9 + +base10Integer :: Parser Integer +base10Integer = do + number <- some parseDigit + let n = foldl (\acc x -> (acc * 10) + (charToDigit x)) 0 number + return n + +-- 3. Extend the parser your wrote to handle negative and positive integers. +-- Try writing a new parser in terms of the one you already have to do this. +base10Integer' :: Parser Integer +base10Integer' = do + negative <- (char '-' >> (return negate)) <|> (return id) + number <- some parseDigit + let n = foldl (\acc x -> (acc * 10) + (charToDigit x)) 0 number + return $ negative n |
