Congratulations. You have just reached the final round of the game show "The Winner Takes It All". All you have to do is to decide is if the order of a line up of items that is presented to you on a conveyer belt follows certain rules. The show master has already acquainted you with them ahead of time (they may vary from game to game) and you are very lucky today! There are only two rules he asked you to memorize.
V K V E TQuickly you see, that sure, that's ok, because the first
V
and K
satisfy two of the tree
requirements of rule 1. Note that rule 1 is not fully applied yet,
because there must be at least one entertainment item among the
remaining articles. The next V
is valid by rule 2, then
comes E
which justifies the application of rule 1. And
the final item T
justifies the second rule. If this were
the real game and not just a practice round, you should have said
YES!.
V V TAs you can quickly see, this order of articles cannot be justified by the two rules from above. You should say NO, and the car, the bike, and the trip to Cancun would have been yours.
The conveyer belt starts rolling and you see
V K V E K EAlso this is a valid order, because it is justified by rule 1 (two applications) .
Back to the homework! Please solve this problem for the general case by writing a Haskell program which decides for you if to say yes or no.
type Rule = [Char] play :: [Rule] -> String -> BoolI am sure, that your solution will return an answer in less time than 30 seconds. You can assume that the rules are formalized as lists of characters
[Char]
, and the items of the conveyer
belt are presented to you as a String
.
Hint: Try to adopt and modify the code for matching regular expressions in class. Do not write more than 40 lines of code! If you do, something is likely to be wrong, or you haven't found an elegant solution.
With this problem we return to the world of algorithms that match regular expressions. In class we have discussed the standard set of operators of regular expressions, and we have discussed an algorithm that decides if a string is matched by a regular expression our not. You are asked to extend the regular expression language by a new constructors that can express negation.
acc "00" (Not (Star (Char "1"))) initshould succeed, because
00
is not an element of Star
(Char "1")
.
To solve this problem we extend the data type Reg
by
the new constructor Not
:
data Reg = Zero | One | Char Char | Plus Reg Reg | Times Reg Reg | Star Reg | Not Reg deriving ShowMathematically, the language generated by this language is defined as
L(Not R) = {s | s is not an element of L(R)}Please extend the acceptor for regular expressions
acc
presented in class to cover this new case.