| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 15 Sep 2023 10:49:33 +0100 | |
| changeset 918 | 19a5d332cb49 | 
| parent 916 | 2ab96407f350 | 
| child 921 | b4da72f2219f | 
| permissions | -rw-r--r-- | 
| 631 | 1 | % !TEX program = xelatex | 
| 0 | 2 | \documentclass{article}
 | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 3 | \usepackage{../style}
 | 
| 0 | 4 | |
| 5 | \begin{document}
 | |
| 6 | ||
| 7 | \section*{Homework 1}
 | |
| 8 | ||
| 331 
a2c18456c6b7
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
294diff
changeset | 9 | \HEADER | 
| 
a2c18456c6b7
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
294diff
changeset | 10 | |
| 0 | 11 | \begin{enumerate}
 | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 12 | |
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 13 | \item {\bf (Optional)} If you want to run the code presented
 | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 14 | in the lectures, install the Scala programming language | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 15 | available (for free) from | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 16 | |
| 743 | 17 |       \begin{center}
 | 
| 18 |         \url{http://www.scala-lang.org}
 | |
| 19 |       \end{center}
 | |
| 20 | ||
| 21 | and the Ammonite REPL from | |
| 22 | ||
| 23 |        \begin{center}
 | |
| 24 |        \url{https://ammonite.io}
 | |
| 25 |        \end{center}      
 | |
| 0 | 26 | |
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 27 | If you want to follow the code I present during the | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 28 | lectures, read the handout about Scala. | 
| 0 | 29 | |
| 639 | 30 | %\item {\bf (Optional)} Have a look at the crawler programs.
 | 
| 31 | % Can you find a usage for them in your daily programming | |
| 32 | % life? Can you improve them? For example in cases there | |
| 33 | % are links that appear on different recursion levels, the | |
| 34 | % crawlers visit such web-pages several times. Can this be | |
| 35 | % avoided? Also, the crawlers flag as problematic any page | |
| 36 | % that gives an error, but probably only 404 Not Found | |
| 37 | % errors should be flagged. Can you change that?) | |
| 104 
ffde837b1db1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
102diff
changeset | 38 | |
| 640 | 39 | \item {\bf (Optional)} Have a look at the catastrophic backtracking
 | 
| 40 | programs uploaded on KEATS. Convince yourself that they really require | |
| 41 | a lot of computation time. If you have similar examples in your own | |
| 42 | favourite programming language, I am happy to hear about it. | |
| 43 | ||
| 44 | ||
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 45 | \item Read the handout of the first lecture and the handout | 
| 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 46 | about notation. Make sure you understand the concepts of | 
| 498 | 47 | strings and languages. In the context of the CFL-course, | 
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 48 |       what is meant by the term \emph{language}?
 | 
| 9 | 49 | |
| 876 | 50 |       \solution{A language - in this context - is just a set of
 | 
| 51 | strings. Some of these sets can actually not be described by | |
| 52 | regular expressions. Only regular​ languages can. This is | |
| 53 | something for lecture 3.} | |
| 54 | ||
| 550 | 55 | \item Give the definition for regular expressions---this is an | 
| 498 | 56 | inductive datatype. What is the | 
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 57 | meaning of a regular expression? (Hint: The meaning is | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 58 | defined recursively.) | 
| 0 | 59 | |
| 876 | 60 |       \solution{Here I would also expect the grammar for basic regular
 | 
| 61 | expressions and the definition of the recursive L-function. Discuss | |
| 62 | differences between $r_1 + r_2$ and $r^+$. Discuss differences between | |
| 63 | ``real-life regexes'' and regexes in this module.} | |
| 64 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 65 | \item Assume the concatenation operation of two strings is | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 66 | written as $s_1 @ s_2$. Define the operation of | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 67 |       \emph{concatenating} two sets of strings. This operation
 | 
| 394 
2f9fe225ecc8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
355diff
changeset | 68 | is also written as $\_ \,@\, \_$. According to | 
| 
2f9fe225ecc8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
355diff
changeset | 69 |       this definition, what is $A \,@\, \{\}$ equal to?
 | 
| 498 | 70 | Is in general $A\,@\,B$ equal to $B\,@\,A$? | 
| 0 | 71 | |
| 876 | 72 |       \solution{ What is $A @ {[]}$? Are there special cases
 | 
| 73 | where $A @ B = B @ A$? } | |
| 74 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 75 | \item Assume a set $A$ contains 4 strings and a set $B$ | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 76 | contains 7 strings. None of the strings is the empty | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 77 | string. How many strings are in $A \,@\, B$? | 
| 249 
377c59df7297
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
227diff
changeset | 78 | |
| 876 | 79 |       \solution{28, but there are corner cases where there are fewer
 | 
| 80 | than 28 elements. Can students think of such corner cases? | |
| 81 |       For example $A = \{a, ab, \ldots\}$, $B = \{bc, c,\ldots\}$ }
 | |
| 82 | ||
| 267 
a1544b804d1e
updated homeworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
258diff
changeset | 83 | \item How is the power of a language defined? (Hint: There are two | 
| 
a1544b804d1e
updated homeworks
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
258diff
changeset | 84 |   rules, one for $\_^0$ and one for $\_^{n+1}$.)
 | 
| 109 
f2a90dda7e3b
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
104diff
changeset | 85 | |
| 876 | 86 |      \solution{Two rules: 0-case and n+1 case.}
 | 
| 87 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 88 | \item Let $A = \{[a], [b], [c], [d]\}$. (1) How many strings
 | 
| 438 
84608b4b3578
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
416diff
changeset | 89 | are in $A^4$? (2) Consider also the case of $A^4$ where one of | 
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 90 | the strings in $A$ is the empty string, for example $A = | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 91 |       \{[a], [b], [c], []\}$.
 | 
| 293 
ca349cfe3474
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
267diff
changeset | 92 | |
| 876 | 93 |       \solution{121 is correct. But make sure you understand why it is 121
 | 
| 94 | in cases you do not have a computer at your fingertips.} | |
| 95 | ||
| 507 | 96 | \item (1) How many basic regular expressions are there to match | 
| 776 | 97 |       \textbf{only} the string $abcd$? (2) How many if they cannot include
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
438diff
changeset | 98 | $\ONE$ and $\ZERO$? (3) How many if they are also not | 
| 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
438diff
changeset | 99 | allowed to contain stars? (4) How many if they are also | 
| 401 
5d85dc9779b1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
394diff
changeset | 100 | not allowed to contain $\_ + \_$? | 
| 0 | 101 | |
| 876 | 102 |       \solution{1-3 are infinite (tell the idea why - examples); 4 is five - remember regexes are trees.}
 | 
| 103 | ||
| 355 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 104 | \item When are two regular expressions equivalent? Can you | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 105 | think of instances where two regular expressions match | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 106 | the same strings, but it is not so obvious that they do? | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 107 | For example $a + b$ and $b + a$ do not count\ldots they | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 108 | obviously match the same strings, namely $[a]$ and | 
| 
a259eec25156
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
331diff
changeset | 109 | $[b]$. | 
| 403 
564f7584eff1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
401diff
changeset | 110 | |
| 876 | 111 |       \solution{for example $r^* = 1 + r \cdot r^*$ for any regular expression $r$.
 | 
| 112 | Can students think about why this is the case?} | |
| 113 | ||
| 416 | 114 | \item What is meant by the notions \emph{evil regular expressions}
 | 
| 726 | 115 |   and by \emph{catastrophic backtracking}?
 | 
| 116 | ||
| 876 | 117 |   \solution{catastrophic backtracking also applies to other regexes, not just $(a^*)^*b$}
 | 
| 118 | ||
| 726 | 119 | \item Given the regular expression $(a + b)^* \cdot b \cdot (a + b)^*$, | 
| 841 | 120 | which of the following regular expressions are equivalent | 
| 726 | 121 | |
| 122 | \begin{center}
 | |
| 123 | \begin{tabular}{ll}    
 | |
| 124 | 1) & $(ab + bb)^* \cdot (a + b)^*$\\ % no | |
| 125 | 2) & $(a + b)^* \cdot (ba + bb + b) \cdot (a + b)^*$\\ % yes | |
| 126 | 3) & $(a + b)^* \cdot (a + b) \cdot (a + b)^*$ % no | |
| 127 | \end{tabular}
 | |
| 128 | \end{center}
 | |
| 876 | 129 | |
| 130 |   \solution{no, yes (why?), no.}
 | |
| 726 | 131 | |
| 403 
564f7584eff1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
401diff
changeset | 132 | |
| 
564f7584eff1
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
401diff
changeset | 133 | \item \POSTSCRIPT | 
| 0 | 134 | \end{enumerate}
 | 
| 135 | ||
| 136 | \end{document}
 | |
| 137 | ||
| 138 | %%% Local Variables: | |
| 139 | %%% mode: latex | |
| 140 | %%% TeX-master: t | |
| 141 | %%% End: |