87 %% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf |
90 %% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf |
88 %% |
91 %% |
89 %% Section 10 about strings; interpolations and multiline strings |
92 %% Section 10 about strings; interpolations and multiline strings |
90 |
93 |
91 |
94 |
92 |
95 % Exact colors from NB |
|
96 \usepackage[breakable]{tcolorbox} |
|
97 \definecolor{incolor}{HTML}{303F9F} |
|
98 \definecolor{outcolor}{HTML}{D84315} |
|
99 \definecolor{cellborder}{HTML}{CFCFCF} |
|
100 \definecolor{cellbackground}{HTML}{F7F7F7} |
|
101 |
|
102 |
|
103 |
93 \begin{document} |
104 \begin{document} |
94 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019, 2020} |
105 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019, 2020} |
|
106 |
|
107 %\begin{tcolorbox}[breakable,size=fbox,boxrule=1pt,pad at break*=1mm,colback=cellbackground,colframe=cellborder] |
|
108 % abd |
|
109 %\end{tcolorbox} |
95 |
110 |
96 \section*{A Crash-Course in Scala} |
111 \section*{A Crash-Course in Scala} |
97 |
112 |
98 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled |
113 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled |
99 \underline{a}cademic \underline{la}nguage''}\smallskip\\ |
114 \underline{a}cademic \underline{la}nguage''}\smallskip\\ |
498 scala> List(1,2,1).size |
515 scala> List(1,2,1).size |
499 scala> Set(1,2,1).size |
516 scala> Set(1,2,1).size |
500 scala> List(1) == List(1) |
517 scala> List(1) == List(1) |
501 scala> Array(1) == Array(1) |
518 scala> Array(1) == Array(1) |
502 scala> Array(1).sameElements(Array(1)) |
519 scala> Array(1).sameElements(Array(1)) |
|
520 \end{lstlisting} |
|
521 |
|
522 \noindent |
|
523 Also observe carefully what Scala responds in the following |
|
524 three instances involving the constant \lstinline!1!---can |
|
525 you explain the differences? |
|
526 |
|
527 |
|
528 \begin{lstlisting}[numbers=none] |
|
529 scala> 1 |
|
530 scala> 1L |
|
531 scala> 1F |
503 \end{lstlisting}\smallskip |
532 \end{lstlisting}\smallskip |
|
533 |
|
534 |
504 |
535 |
505 \noindent |
536 \noindent |
506 Please take the Scala REPL seriously: If you want to take advantage of my |
537 Please take the Scala REPL seriously: If you want to take advantage of my |
507 reference implementation for the assignments, you will need to be |
538 reference implementation for the assignments, you will need to be |
508 able to ``play around'' with it! |
539 able to ``play around'' with it! |
657 \end{lstlisting} |
688 \end{lstlisting} |
658 |
689 |
659 \noindent |
690 \noindent |
660 but this seems a bit overkill for a small function like \code{fact}. |
691 but this seems a bit overkill for a small function like \code{fact}. |
661 Note that Scala does not have a \code{then}-keyword in an |
692 Note that Scala does not have a \code{then}-keyword in an |
662 \code{if}-statement; and there should be always an \code{else}-branch. |
693 \code{if}-statement. Also important is that there should be always an |
663 Never write an \code{if} without an \code{else}, unless you know what |
694 \code{else}-branch. Never write an \code{if} without an \code{else}, |
664 you are doing! Note also that there are a few other ways of how to |
695 unless you know what you are doing! While \code{def} is the main |
665 define a function. We will see some of them in the next sections. |
696 mechanism for defining functions, there are a few other ways for doing |
|
697 this. We will see some of them in the next sections. |
666 |
698 |
667 Before we go on, let me explain one tricky point in function |
699 Before we go on, let me explain one tricky point in function |
668 definitions, especially in larger definitions. What does a Scala function |
700 definitions, especially in larger definitions. What does a Scala |
669 actually return? Scala has a \code{return} keyword, but it is |
701 function return as result? Scala has a \code{return} keyword, but it is |
670 used for something different than in Java (and C/C++). Therefore please |
702 used for something different than in Java (and C/C++). Therefore please |
671 make sure no \code{return} slips into your Scala code. |
703 make sure no \code{return} slips into your Scala code. |
672 |
704 |
673 So in the absence of \code{return}, what value does a Scala function |
705 So in the absence of \code{return}, what value does a Scala function |
674 actually produce? A rule-of-thumb is whatever is in the last line of the |
706 actually produce? A rule-of-thumb is whatever is in the last line of the |
685 \end{lstlisting} |
717 \end{lstlisting} |
686 |
718 |
687 \noindent In this example the expression \code{s / n} is in the last |
719 \noindent In this example the expression \code{s / n} is in the last |
688 line of the function---so this will be the result the function |
720 line of the function---so this will be the result the function |
689 calculates. The two lines before just calculate intermediate values. |
721 calculates. The two lines before just calculate intermediate values. |
690 This principle of the ``last-line'' comes in handy when you need to print |
722 This principle of the ``last-line'' comes in handy when you need to |
691 out values, for example, for debugging purposes. Suppose you want |
723 print out values, for example, for debugging purposes. Suppose you want |
692 rewrite the function as |
724 rewrite the function as |
693 |
725 |
694 \begin{lstlisting}[numbers=none] |
726 \begin{lstlisting}[numbers=none] |
695 def average(xs: List[Int]) : Int = { |
727 def average(xs: List[Int]) : Int = { |
696 val s = xs.sum |
728 val s = xs.sum |
704 \noindent |
736 \noindent |
705 Here the function still only returns the expression in the last line. |
737 Here the function still only returns the expression in the last line. |
706 The \code{println} before just prints out some information about the |
738 The \code{println} before just prints out some information about the |
707 input of this function, but does not contribute to the result of the |
739 input of this function, but does not contribute to the result of the |
708 function. Similarly, the value \code{h} is used in the \code{println} |
740 function. Similarly, the value \code{h} is used in the \code{println} |
709 but does not contribute to what integer is returned. However note that |
741 but does not contribute to what integer is returned. |
710 the idea with the ``last line'' is only a rough rule-of-thumb. A better |
742 |
711 rule might be: the last expression that is evaluated in the function. |
743 A caveat is that the idea with the ``last line'' is only a rough |
712 Consider the following version of \code{iaverage}: |
744 rule-of-thumb. A better rule might be: the last expression that is |
|
745 evaluated in the function. Consider the following version of |
|
746 \code{average}: |
713 |
747 |
714 \begin{lstlisting}[numbers=none] |
748 \begin{lstlisting}[numbers=none] |
715 def average(xs: List[Int]) : Int = { |
749 def average(xs: List[Int]) : Int = { |
716 if (xs.length == 0) 0 |
750 if (xs.length == 0) 0 |
717 else xs.sum / xs.length |
751 else xs.sum / xs.length |
718 } |
752 } |
719 \end{lstlisting} |
753 \end{lstlisting} |
720 |
754 |
721 \noindent |
755 \noindent |
722 What does this function return? Well are two possibilities: either the |
756 What does this function return? Well there are two possibilities: either |
723 result of \code{xs.sum / xs.length} in the last line provided the list |
757 the result of \code{xs.sum / xs.length} in the last line provided the |
724 \code{xs} is nonempty, \textbf{or} if the list is empty, then it will |
758 list \code{xs} is nonempty, \textbf{or} if the list is empty, then it |
725 return \code{0} from the \code{if}-branch (which is technically not the |
759 will return \code{0} from the \code{if}-branch (which is technically not |
726 last line, but the last expression evaluated by the function in the |
760 the last line, but the last expression evaluated by the function in the |
727 empty-case). |
761 empty-case). |
728 |
762 |
729 Summing up, do not use \code{return} in your Scala code! A function |
763 Summing up, do not use \code{return} in your Scala code! A function |
730 returns what is evaluated by the function as the last expression. There |
764 returns what is evaluated by the function as the last expression. There |
731 is always only one such last expression. Previous expressions might |
765 is always only one such last expression. Previous expressions might |