3 ms·
> returning an offset past the end of the string I feel if you're dealing with offsets into strings, it's probably easier to stop doing that rather than try to
by mrkeen 3y ago
> returning an offset past the end of the string
I feel if you're dealing with offsets into strings, it's probably easier to stop doing that rather than try to get the type checker to prove it 'correct'.
I tried a linear types implementation:
newtype Parser a = P (String ⊸ (String, Maybe a))
parseChar :: Char -> Parser Char
parseChar c = P $ \s ->
case s of
[] -> ([], Nothing)
(x:xs) -> if x == c
then (xs, Just c)
else (xs, Nothing)
This guarantees that the 's' is used exactly once instead of dropped on the floor. It's not great because I wanted to drop it on the floor.
Here's it not compiling when I use the wrong string:
...
then ("some string", Just c)
...
• Couldn't match type ‘'Many’ with ‘'One’
arising from multiplicity of ‘s’
• In the second argument of ‘($)’, namely
‘\ s
-> case s of