Binary file cws/cw05.pdf has changed
--- a/cws/cw05.tex Wed Sep 17 15:11:48 2025 +0100
+++ b/cws/cw05.tex Wed Sep 17 16:30:09 2025 +0100
@@ -70,11 +70,12 @@
You will be marked according to the input files
\begin{itemize}
-\item\href{https://nms.kcl.ac.uk/christian.urban/cfl/progs/sqr.fun}{sqr.fun}
-\item\href{https://nms.kcl.ac.uk/christian.urban/cfl/progs/fact.fun}{fact.fun}
-\item\href{https://nms.kcl.ac.uk/christian.urban/cfl/progs/mand.fun}{mand.fun}
-\item\href{https://nms.kcl.ac.uk/christian.urban/cfl/progs/mand2.fun}{mand2.fun}
-\item\href{https://nms.kcl.ac.uk/christian.urban/cfl/progs/hanoi.fun}{hanoi.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/sqr.fun}{sqr.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/fact.fun}{fact.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/mand.fun}{mand.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/mand2.fun}{mand2.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/hanoi.fun}{hanoi.fun}
+\item\href{https://cflmark.nms.kcl.ac.uk/hg/afl-material/raw-file/tip/progs/sqr.fun}{sqr.fun}
\end{itemize}
\noindent
@@ -86,9 +87,9 @@
effort. You have not copied from anyone else. An exception is the
Scala code I showed during the lectures or uploaded to KEATS, which
you can both use. You can also use your own code from the CW~1 --
-CW~4. But do not
-be tempted to ask Github Copilot for help or do any other
-shenanigans like this!
+CW~4. %But do not
+%be tempted to ask Github Copilot for help or do any other
+%shenanigans like this!
\subsection*{Task}
@@ -281,11 +282,14 @@
looking at the literature which solves the problem
with much heavier machinery.
-\item \textbf{Build-In Functions}: The `prelude' comes
- with several build-in functions: \texttt{new\_line()},
+\item \textbf{Built-In Functions}: The `prelude' comes
+ with several built-in functions: \texttt{new\_line()},
\texttt{skip}, \texttt{print\_int(n)}, \texttt{print\_space()},
- \texttt{print\_star()} and \texttt{print\_char(n)}. You can find the `prelude' for
- example in the file \texttt{sqr.ll}.
+ \texttt{print\_star()} as well as \texttt{print\_char(n)}. You
+ can find the `prelude' for
+ example in the file \texttt{sqr.ll}. When printing strings, you
+ can assume programs only contain string \emph{constants}. (see
+ for example sqr.fun and hanoi.fun).
\end{itemize}
\end{document}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/fact.fun Wed Sep 17 16:30:09 2025 +0100
@@ -0,0 +1,21 @@
+// a simple factorial program
+// (including a tail recursive version)
+
+
+def fact(n: Int) : Int =
+ if n == 0 then 1 else n * fact(n - 1);
+
+def facT(n: Int, acc: Int) : Int =
+ if n == 0 then acc else facT(n - 1, n * acc);
+
+def facTi(n: Int) : Int = facT(n, 1);
+
+def top() : Void = {
+ print_int(fact(6));
+ print_char(',');
+ print_int(facTi(6));
+ print_char('\n')
+};
+
+top()
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/hanoi.fun Wed Sep 17 16:30:09 2025 +0100
@@ -0,0 +1,13 @@
+// Towers of Hanoi in Fun
+
+def hanoi(n: Int, a: Int, b: Int, c: Int) : Void =
+ if n != 0 then {
+ hanoi(n - 1, a, c, b);
+ print_int(a);
+ print_string("->"); // prints out "->"
+ print_int(b);
+ print_char('\n');
+ hanoi(n - 1, c, b, a)
+ } else skip();
+
+hanoi(4,1,2,3)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/mand.fun Wed Sep 17 16:30:09 2025 +0100
@@ -0,0 +1,36 @@
+// Mandelbrot program (without character constants)
+
+val Ymin: Double = -1.3;
+val Ymax: Double = 1.3;
+val Ystep: Double = 0.05; //0.025;
+
+val Xmin: Double = -2.1;
+val Xmax: Double = 1.1;
+val Xstep: Double = 0.02; //0.01;
+
+val Maxiters: Int = 1000;
+
+def m_iter(m: Int, x: Double, y: Double,
+ zr: Double, zi: Double) : Void = {
+ if Maxiters <= m
+ then print_star()
+ else {
+ if 4.0 <= zi*zi+zr*zr then print_space()
+ else m_iter(m + 1, x, y, x+zr*zr-zi*zi, 2.0*zr*zi+y)
+ }
+};
+
+def x_iter(x: Double, y: Double) : Void = {
+ if x <= Xmax
+ then { m_iter(0, x, y, 0.0, 0.0) ; x_iter(x + Xstep, y) }
+ else skip()
+};
+
+def y_iter(y: Double) : Void = {
+ if y <= Ymax
+ then { x_iter(Xmin, y) ; new_line() ; y_iter(y + Ystep) }
+ else skip()
+};
+
+
+y_iter(Ymin)
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/mand2.fun Wed Sep 17 16:30:09 2025 +0100
@@ -0,0 +1,36 @@
+// Mandelbrot program (with character constants)
+
+val Ymin: Double = -1.3;
+val Ymax: Double = 1.3;
+val Ystep: Double = 0.05; //0.025;
+
+val Xmin: Double = -2.1;
+val Xmax: Double = 1.1;
+val Xstep: Double = 0.02; //0.01;
+
+val Maxiters: Int = 1000;
+
+def m_iter(m: Int, x: Double, y: Double,
+ zr: Double, zi: Double) : Void = {
+ if Maxiters <= m
+ then print_char(' ')
+ else {
+ if 4.0 <= zi*zi+zr*zr then print_char('0' + (m % 10))
+ else m_iter(m + 1, x, y, x+zr*zr-zi*zi, 2.0*zr*zi+y)
+ }
+};
+
+def x_iter(x: Double, y: Double) : Void = {
+ if x <= Xmax
+ then { m_iter(0, x, y, 0.0, 0.0) ; x_iter(x + Xstep, y) }
+ else skip()
+};
+
+def y_iter(y: Double) : Void = {
+ if y <= Ymax
+ then { x_iter(Xmin, y) ; print_char('\n') ; y_iter(y + Ystep) }
+ else skip()
+};
+
+
+y_iter(Ymin)
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/sqr.fun Wed Sep 17 16:30:09 2025 +0100
@@ -0,0 +1,15 @@
+val Max : Int = 10;
+
+def sqr(x: Int) : Int = x * x;
+
+def all(n: Int) : Void = {
+ if n <= Max
+ then { print_int(sqr(n)) ; new_line(); all(n + 1) }
+ else skip()
+};
+
+{
+ print_string("Squares");
+ new_line();
+ all(0)
+}
\ No newline at end of file