I'm a total Haskell beginner who just discovered that read
spits out an exception when given a decimal number starting with .
rather than a digit. For example, in ghci:
Prelude> read ".7" :: Float
*** Exception: Prelude.read: no parse
I found one discussion and it makes sense why surrounding .
in numbers with digits is required in Haskell. Another discussion is also somewhat helpful, but no one provides a solution of how to actually convert ".7"
to 0.7
.
So, I'm trying to extract data from a fixed-width format file containing fields with values like .7
---is there a standard function or approach I can use to clean this up to a float 0.7
?
(Before I hit this issue, my basic ideas was to define a custom type for my data, use splitWidth
in Data.List.Split
to split each line into its fields, and then use read
to convert each field into its correct type, trying to apply the functional goodness in this answer in the actual implementation.)
As Thomas M. DuBuisson answered in a comment above, the obvious thing to do is myRead = read . ('0':) :: String -> Float
. This works for me --- I won't ever be trying to read negative numbers, and I know which fields should be read as float. Thanks!