I'm attempting to replace numbers in a string with twice their value using Haskell's Text.RE.TDFA package. My current approach:
s *=~/ fmap (\x -> show $ 2.0*(read x::Double)) [ed|${doublestring}([0-9]*\.[0-9]*)///${doublestring}|]
Behavior Observed:
s as "xxx1.1xxx", I get the error:"regex.hs: Prelude.read: no parse"${doublestring} after /// with a literal number (e.g., 1.1), it works as expected:"xxx1.1xxx" → Output: "xxx2.2xxx"Questions:
${doublestring} capture fail during parsing?The fuctor acts on the template, not on the variables in the template, indeed:
data SearchReplace re s =
SearchReplace
{ getSearch :: !re -- ^ the RE to match a string to replace
, getTemplate :: !s -- ^ the replacement template with ${cap}
-- used to identify a capture (by number or
-- name if one was given) and '$$' being
-- used to escape a single '$'
}
deriving (Show)
instance Functor (SearchReplace re) where
fmap f (SearchReplace re x) = SearchReplace re (f x)
This means that your function is given "${doublestring}" as string. We can confirm this by using a trace:
ghc> "1.1" *=~/ fmap (\x -> show (traceShow x (read x)::Double)) [ed|${doublestring}([0-9]*\.[0-9]*)///${doublestring}|]
""${doublestring}"
We thus never get the value to parse, we get the template to handle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With