wsheets/wsh02.tex
changeset 445 b73e7ce91c10
parent 444 7a0735db4788
child 447 f51e593903ac
equal deleted inserted replaced
444:7a0735db4788 445:b73e7ce91c10
       
     1 % !TEX program = xelatex
       
     2 \documentclass{article}
       
     3 \usepackage{../styles/style}
       
     4 \usepackage{../styles/langs}
       
     5 \usepackage{tikz}
       
     6 \usepackage{pgf}
       
     7 \usepackage{marvosym}
       
     8 \usepackage{boxedminipage}
       
     9 
       
    10 \lstset{escapeinside={/*!}{!*/}}
       
    11 \newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
       
    12 
       
    13 \usepackage{menukeys}
       
    14 
       
    15 
       
    16 % Exact colors from NB
       
    17 \usepackage[breakable]{tcolorbox}
       
    18 \definecolor{incolor}{HTML}{303F9F}
       
    19 \definecolor{outcolor}{HTML}{D84315}
       
    20 \definecolor{cellborder}{HTML}{CFCFCF}
       
    21 \definecolor{cellbackground}{HTML}{F7F7F7}
       
    22 
       
    23 
       
    24     
       
    25 \begin{document}
       
    26 \fnote{\copyright{} Christian Urban, King's College London, 2022}
       
    27 
       
    28 \section*{Scala Worksheet 2}
       
    29 
       
    30 
       
    31 
       
    32 \subsection*{Task 1 (Options)}
       
    33 
       
    34 Get familiar with constructing strings and printing strings
       
    35 (i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots)
       
    36 
       
    37 \begin{lstlisting}[numbers=none]
       
    38 scala> List(7,2,3,4,5,6).find(_ < 4)
       
    39 scala> List(5,6,7,8,9).find(_ < 4)
       
    40 scala> 
       
    41 \end{lstlisting}
       
    42 
       
    43 \subsection*{Task 2 (URLs / Files)}
       
    44 
       
    45 ALso Try
       
    46 
       
    47 \begin{lstlisting}[numbers=none]
       
    48 scala> 
       
    49 scala> 
       
    50 scala> 
       
    51 \end{lstlisting}
       
    52 
       
    53 \subsection*{Task 3 (Higher-Order Functions)}
       
    54 
       
    55 Make sure you understand if-conditions in Scala: They are
       
    56 \emph{expressions}, meaning they need to calculate a result. For
       
    57 example an \texttt{if} without an else-branch is nearly always
       
    58 \emph{not} what you intend to write (because you are not meant to
       
    59 change any ``state'' outside the expression). Also, remember the quirks
       
    60 in Scala with if-conditions needing parentheses and there is no
       
    61 \texttt{then}-keyword.
       
    62 
       
    63 \begin{lstlisting}[numbers=none]
       
    64 scala> val s1 = "foo"
       
    65 scala> val s2 = "bar"
       
    66 scala  val s3 = "foobar"
       
    67 scala> if (s1 == s2) print("equal") else print("unequal")
       
    68 scala> if (s1 ++ s2 == s3) print("equal") else print("unequal")
       
    69 \end{lstlisting}
       
    70 
       
    71 \subsection*{Task 4 (Maps)}
       
    72 
       
    73 Write \texttt{for}-comprehensions that enumerate all triples containing the numbers 1 - 5. That is,
       
    74 print the list
       
    75 
       
    76 \[
       
    77 \texttt{(1,1,1)}, \texttt{(2,1,1)}, \texttt{(3,1,1)} \;\ldots\; \texttt{(5,5,5)}
       
    78 \]
       
    79 
       
    80 \noindent
       
    81 Modify the \texttt{for}-comprehensions such that only triples are
       
    82 printed where the sum is divisible by 3.  Then sort the list according
       
    83 to the middle element (for this use \texttt{sortBy}).
       
    84 
       
    85 \subsection*{Task 5 (Pattern-Matching)}
       
    86 
       
    87 \subsection*{Task 6 (Web-Crawler)}
       
    88 
       
    89 Write a pretty print function for lists of integers which
       
    90 ``condenses'' elements in the list, meaning if there is a number
       
    91 several times in a row, then print out \mbox{\texttt{n x e}}, where
       
    92 \texttt{n} is the number of repetitions and \texttt{e} is the
       
    93 number. For example
       
    94 
       
    95 \begin{lstlisting}[numbers=none]
       
    96   List(1,1,1,2,3,3)    =>   List(3 x 1, 2, 2 x 3)
       
    97   List(1,2,3,4,4,4)    =>   List(1, 2, 3, 3 x 4)
       
    98   List(1,1,1,1,1,1)    =>   List(6 x 1)
       
    99   List(1,1,1,2,1,1)    =>   List(3 x 1, 2, 2 x 1)
       
   100 \end{lstlisting}
       
   101 
       
   102 You might like to define a separate function that first counts the occurences
       
   103 for each element and returns a list of (Int, Int)-pairs .
       
   104 
       
   105 \end{document}
       
   106 
       
   107 %%% Local Variables: 
       
   108 %%% mode: latex
       
   109 %%% TeX-master: t
       
   110 %%% End: