The following machine accepts C-style strings with a
representing any non-special character, Q
representing a double-quote
mark, and B
representing a backslash (\
). Thus
the string "It's \"here\"!"
would be written
"QaaaaaBQaaaaBQaQ".
isString.hs
. Be sure to
read the comments in that file!
Testing isString
:
ghci> :load isString.hs [1 of 1] Compiling Main ( isString.hs, interpreted ) Ok, one module loaded. ghci> isString "QaQ" True ghci> isString "QaQa" False ghci> isString "QaQQa" False ghci> isString "QaBQa" False ghci> isString "QaBQaQ" True
An alternative implementation based on using patterns with function
arguments
is isStringWithPatterns.hs
This version will crash on a string that is not in the set ('a' | 'Q'
| 'b')*
.