hws/hw08.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 29 Sep 2022 20:54:02 +0100
changeset 876 771396fa6cc4
parent 726 fba480bbc9f7
child 901 33cff35bdc1a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
     2
\usepackage{../style}
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
     3
\usepackage{../langs}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
     4
\usepackage{../graphics}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 60
diff changeset
     8
\section*{Homework 8}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    10
\HEADER
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    11
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\begin{enumerate}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    13
\item Write a program in the WHILE-language that calculates
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    14
      the factorial function.
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    15
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    16
\item What optimisations could a compiler perform when
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    17
      compiling a WHILE-program?
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    18
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    19
\item What is the main difference between the Java assembler
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    20
      (as processed by Jasmin) and Java Byte Code?
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    21
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    22
\item Consider the following Scala snippet. Are the two functions
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    23
\texttt{is\_even} and \texttt{is\_odd} tail-recursive?     
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    24
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    25
\begin{lstlisting}[numbers=none]
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    26
def iseven(n: Int) : Boolean = {
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    27
    if (n == 0) true else isodd(n - 1)
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    28
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    29
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    30
def isodd(n: Int) : Boolean = {
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    31
    if (n == 0) false 
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    32
    else if (n == 1) true else iseven(n - 1)
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    33
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    34
\end{lstlisting}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    35
      
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    36
Do they cause stack-overflows when compiled to the JVM (for example by Scala)?
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    37
530
cec95ad3a837 updated
cu
parents: 359
diff changeset
    38
cec95ad3a837 updated
cu
parents: 359
diff changeset
    39
\item Explain what is meant by the terms lazy evaluation and eager
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    40
  evaluation.
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    41
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    42
\item \POSTSCRIPT    
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
%%% End: