[Haskell-cafe] Alex problem, help needed!!!!!!!!! ASAP :)

Niklas Broberg niklas.broberg at gmail.com
Sat Oct 3 17:07:24 EDT 2009

> $digit+                                 { \p s -> TokenEntero p (read s) }
> \' [$alpha $digit \_]* \'            { \p s -> TokenString p (read s)}
> [$digit]+\.[$digit]+                 { \p s -> TokenDouble p (read s) }
> $alpha [$alpha $digit \_]*        { \p s -> TokenVar p (read s) }

You are using 'read' in slightly strange ways here. What read does is
to take a string of characters that supposedly represents a value of
some type, and tries to create a value of said type from the string.

On the first and third lines you read a sequence of digits to an
integer resp double, which is fine, the strings you pass to 'read' are
representations of integers and doubles resp.

On the second line you're trying to read something of the form
'hello', i.e. surrounded by single-quotes, as a Haskell String. But
this doesn't represent a Haskell String, which is what 'read' expects.
Haskell Strings have double-quotes, so there will be no proper 'read',
if you want single-quotes you need to define your own function to do
the conversion (hint: init . tail).

On the last line, which is the one giving you the error here, you're
again trying to 'read' a string of characters into a String. And
again, Haskell Strings are surrounded by double-quotes, so 'read' will
not find a representation. This time the fix is even simpler. :-)

Prelude> read "hello" :: String
"*** Exception: Prelude.read: no parse
Prelude> read "\"hello\"" :: String



More information about the Haskell-Cafe mailing list