diff -r cdfa6a293453 -r 126d0e47ac85 cws/main_cw05.tex --- a/cws/main_cw05.tex Tue Nov 01 15:03:48 2022 +0000 +++ b/cws/main_cw05.tex Wed Nov 02 13:22:59 2022 +0000 @@ -15,7 +15,7 @@ \begin{document} -\section*{Main Part 5 (Scala, 9 Marks)} +\section*{Main Part 5 (Scala, 10 Marks)} \mbox{}\hfill\textit{``If there's one feature that makes Scala, `Scala',}\\ \mbox{}\hfill\textit{ I would pick implicits.''}\smallskip\\ @@ -89,7 +89,7 @@ \newpage -\subsection*{Part A (5 Marks)} +\subsection*{Part A (6 Marks)} Coming from Java or C++, you might think Scala is a rather esoteric programming language. But remember, some serious companies have built @@ -109,7 +109,7 @@ language has been shown to be Turing complete\ldots{}if this doesn't ring any bell with you: it roughly means that every(!) algorithm can, in principle, be implemented in brainf***. It just takes a lot of -determination and quite a lot of memory resources. +determination and quite a lot of memory and time. Some relatively sophisticated sample programs in brainf*** are given in the file \texttt{bf.scala}, including a brainf*** program for the @@ -391,8 +391,26 @@ program counter \texttt{pc}, the memory pointer \texttt{mp} and the memory \texttt{mem}.\label{comms}} \end{figure} -\end{itemize}\bigskip + +\item[(5)] Let us also generate some BF programs ourselves: + Write a function + \texttt{generate} which takes a list of characters as input and generates + a corresponding BF program that prints out this list of characters. One + way to generate such a program is to consider each character in the list, + add as many \texttt{"+"} given by the ASCII code of the character, then add the + \texttt{"."} command for printing and add the loop \texttt{"[-]"} for ``zero-ing'' the memory + cell; then go to the next character in the list. For example + the list \texttt{"ABC".toString} produces (as a single string) + + \begin{center} + \texttt{+++++++++++++++++++++++++++++++++++++++++++++++++++++} + \texttt{++++++++++++.[-]+++++++++++++++++++++++++++++++++++++} + \texttt{+++++++++++++++++++++++++++++.[-]++++++++++++++++++++} + \texttt{+++++++++++++++++++++++++++++++++++++++++++++++.[-]} + \end{center}\mbox{}\hfill[1 Mark] + +\end{itemize}\bigskip %%\newpage \subsection*{Part B (4 Marks)} @@ -405,7 +423,7 @@ \subsubsection*{Tasks (file bfc.scala)} \begin{itemize} -\item[(5)] Compilers, in general, attempt to make programs run +\item[(6)] Compilers, in general, attempt to make programs run faster by precomputing as much information as possible before running the program. In our case we can precompute the addresses where we need to jump at the beginning and end of @@ -448,7 +466,7 @@ the function \texttt{jumpLeft} and \texttt{jumpRight} for calculating the \texttt{jtable}.\hfill{[1 Mark]} -\item[(6)] Compilers try to eliminate any ``dead'' code that could +\item[(7)] Compilers try to eliminate any ``dead'' code that could slow down programs and also perform what is often called \emph{peephole optimisations}.\footnote{\url{https://en.wikipedia.org/wiki/Peephole_optimization}} @@ -476,7 +494,7 @@ with new strings.\\ \mbox{}\hfill{[1 Mark]} -\item[(7)] Finally, real compilers try to take advantage of modern +\item[(8)] Finally, real compilers try to take advantage of modern CPUs which often provide complex operations in hardware that can combine many smaller instructions into a single faster instruction.