36 \begin{lstlisting}[numbers=none] |
36 \begin{lstlisting}[numbers=none] |
37 scala> 2 + 3 |
37 scala> 2 + 3 |
38 res0: Int = 5 |
38 res0: Int = 5 |
39 \end{lstlisting} |
39 \end{lstlisting} |
40 |
40 |
41 \noindent indicating that the result of the addition is of |
41 \noindent indicating that the result of the addition is of type |
42 type \code{Int} and the actual result is 5. Another classic |
42 \code{Int} and the actual result is 5; \code{res0} is a name that |
43 example you can try out is |
43 Scala gives automatically to the result. Yoy can reuse this name later |
|
44 on. Another classic example you can try out is |
44 |
45 |
45 \begin{lstlisting}[numbers=none] |
46 \begin{lstlisting}[numbers=none] |
46 scala> print("hello world") |
47 scala> print("hello world") |
47 hello world |
48 hello world |
48 \end{lstlisting} |
49 \end{lstlisting} |
49 |
50 |
50 \noindent Note that in this case there is no result. The |
51 \noindent Note that in this case there is no result. The |
51 reason is that \code{print} does not actually produce a result |
52 reason is that \code{print} does not actually produce a result |
52 (there is no \code{resXX} and no type), rather it is a |
53 (there is no \code{resX} and no type), rather it is a |
53 function that causes the \emph{side-effect} of printing out a |
54 function that causes the \emph{side-effect} of printing out a |
54 string. Once you are more familiar with the functional |
55 string. Once you are more familiar with the functional |
55 programming-style, you will know what the difference is |
56 programming-style, you will know what the difference is |
56 between a function that returns a result, like addition, and a |
57 between a function that returns a result, like addition, and a |
57 function that causes a side-effect, like \code{print}. We |
58 function that causes a side-effect, like \code{print}. We |
58 shall come back to this point later, but if you are curious |
59 shall come back to this point later, but if you are curious |
59 now, the latter kind of functions always has \code{Unit} as |
60 now, the latter kind of functions always has \code{Unit} as |
60 return type. |
61 return type. It is just not printed. |
61 |
62 |
62 You can try more examples with the Scala interpreter, but try |
63 You can try more examples with the Scala interpreter, but try |
63 first to guess what the result is (not all answers by Scala are obvious): |
64 first to guess what the result is (not all answers by Scala are obvious): |
64 |
65 |
65 \begin{lstlisting}[numbers=none] |
66 \begin{lstlisting}[numbers=none] |
73 scala> true && false |
74 scala> true && false |
74 scala> 1 > 1.0 |
75 scala> 1 > 1.0 |
75 scala> "12345".length |
76 scala> "12345".length |
76 \end{lstlisting} |
77 \end{lstlisting} |
77 |
78 |
78 \subsection*{Stand-Alone Apps} |
79 \subsection*{Stand-Alone Scala Apps} |
79 |
80 |
80 If you want to write a stand-alone app in Scala, you can |
81 If you want to write a stand-alone app in Scala, you can |
81 implement an object that is an instance of \code{App}, say |
82 implement an object that is an instance of \code{App}, say |
82 |
83 |
83 \begin{lstlisting}[numbers=none] |
84 \begin{lstlisting}[numbers=none] |
84 object Hello extends App { |
85 object Hello extends App { |
85 println("hello world") |
86 println("hello world") |
86 } |
87 } |
87 \end{lstlisting} |
88 \end{lstlisting} |
88 |
89 |
89 \noindent save it in a file, say {\tt hello-world.scala}, and |
90 \noindent save it in a file, for example {\tt hello-world.scala}, and |
90 then run the compiler and runtime environment: |
91 then run the compiler and runtime environment: |
91 |
92 |
92 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
93 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
93 $ scalac hello-world.scala |
94 $ scalac hello-world.scala |
94 $ scala Hello |
95 $ scala Hello |
95 hello world |
96 hello world |
96 \end{lstlisting} |
97 \end{lstlisting} |
97 |
98 |
|
99 \noindent |
98 Like Java, Scala targets the JVM and consequently |
100 Like Java, Scala targets the JVM and consequently |
99 Scala programs can also be executed by the bog-standard Java |
101 Scala programs can also be executed by the bog-standard Java |
100 Runtime. This only requires the inclusion of {\tt |
102 Runtime. This only requires the inclusion of {\tt |
101 scala-library.jar}, which on my computer can be done as |
103 scala-library.jar}, which on my computer can be done as |
102 follows: |
104 follows: |
110 \noindent You might need to adapt the path to where you have |
112 \noindent You might need to adapt the path to where you have |
111 installed Scala. |
113 installed Scala. |
112 |
114 |
113 \subsection*{Values} |
115 \subsection*{Values} |
114 |
116 |
115 In the lectures, I will try as much as possible to avoid the term |
117 In the lectures I will try to avoid as much as possible the term |
116 \emph{variables} familiar from other programming languages. Scala |
118 \emph{variables} familiar from other programming languages. The reason |
117 has \emph{values}, which can be seen as abbreviations of larger |
119 is that Scala has \emph{values}, which can be seen as abbreviations of |
118 expressions. For example |
120 larger expressions. For example |
119 |
121 |
120 \begin{lstlisting}[numbers=none] |
122 \begin{lstlisting}[numbers=none] |
121 scala> val x = 42 |
123 scala> val x = 42 |
122 x: Int = 42 |
124 x: Int = 42 |
123 |
125 |
128 z: Int = 6 |
130 z: Int = 6 |
129 \end{lstlisting} |
131 \end{lstlisting} |
130 |
132 |
131 \noindent |
133 \noindent |
132 Why the kerfuffle about values? Well, values are \emph{immutable}. You cannot |
134 Why the kerfuffle about values? Well, values are \emph{immutable}. You cannot |
133 change their value after you defined them. If you try to reassign, say, |
135 change their value after you defined them. If you try to reassign |
134 \code{z}, Scala will yell at you: |
136 \code{z} above, Scala will yell at you: |
135 |
137 |
136 \begin{lstlisting}[numbers=none] |
138 \begin{lstlisting}[numbers=none] |
137 scala> z = 9 |
139 scala> z = 9 |
138 error: reassignment to val |
140 error: reassignment to val |
139 z = 9 |
141 z = 9 |
149 scala> val z = x / 7 |
151 scala> val z = x / 7 |
150 scala> val x = 70 |
152 scala> val x = 70 |
151 scala> println(z) |
153 scala> println(z) |
152 \end{lstlisting} |
154 \end{lstlisting} |
153 |
155 |
154 \noindent but try to guess what Scala will print out in the code above |
156 \noindent but try to guess what Scala will print out |
155 for \code{z}? Will it be \code{6} or \code{10}? A final word about |
157 for \code{z}? Will it be \code{6} or \code{10}? A final word about |
156 values: Try to stick to the convention that names of values should be |
158 values: Try to stick to the convention that names of values should be |
157 lower case, like \code{x}, \code{y}, \code{foo41} and so on. |
159 lower case, like \code{x}, \code{y}, \code{foo41} and so on. |
158 |
160 |
159 |
161 |
160 \subsection*{Function Definitions} |
162 \subsection*{Function Definitions} |
161 |
163 |
162 A function \code{f} taking a single argument of type \code{Int} can be defined |
164 We do functional programming. So defining functions will be our main occupation. |
|
165 A function \code{f} taking a single argument of type \code{Int} can be defined in Scala |
163 as follows: |
166 as follows: |
164 |
167 |
165 \begin{lstlisting}[numbers=none] |
168 \begin{lstlisting}[numbers=none] |
166 def f(x: Int) : String = EXPR |
169 def f(x: Int) : String = EXPR |
167 \end{lstlisting} |
170 \end{lstlisting} |
168 |
171 |
169 \noindent |
172 \noindent |
170 It returns the value resulting from evaluating the expression |
173 This function returns the value resulting from evaluating the expression |
171 \code{EXPR} (whatever is substituted for this). The result will be |
174 \code{EXPR} (whatever is substituted for this). The result will be |
172 of type \code{String}. Simple examples of Scala functions are: |
175 of type \code{String}. It is a good habbit to include this information |
|
176 about the return type always. Simple examples of Scala functions are: |
173 |
177 |
174 \begin{lstlisting}[numbers=none] |
178 \begin{lstlisting}[numbers=none] |
175 def incr(x: Int) : Int = x + 1 |
179 def incr(x: Int) : Int = x + 1 |
176 def double(x: Int) : Int = x + x |
180 def double(x: Int) : Int = x + x |
177 def square(x: Int) : Int = x * x |
181 def square(x: Int) : Int = x * x |
186 } |
190 } |
187 \end{lstlisting} |
191 \end{lstlisting} |
188 |
192 |
189 \noindent |
193 \noindent |
190 where each argument requires its type and the result type of the |
194 where each argument requires its type and the result type of the |
191 function, \code{rty}, shoudl be given. If the body of the function |
195 function, \code{rty}, should be given. If the body of the function is |
192 is more complex, then it can be enclosed in braces; it it is just a |
196 more complex, then it can be enclosed in braces, like above. If it it |
193 simple expression, like \code{x + 1}, you can omit the braces. Very |
197 is just a simple expression, like \code{x + 1}, you can omit the |
194 often functions are recursive (call themselves) like |
198 braces. Very often functions are recursive (call themselves) like |
|
199 the venerable factorial function. |
195 |
200 |
196 \begin{lstlisting}[numbers=none] |
201 \begin{lstlisting}[numbers=none] |
197 def fact(n: Int): Int = |
202 def fact(n: Int): Int = |
198 if (n == 0) 1 else n * fact(n - 1) |
203 if (n == 0) 1 else n * fact(n - 1) |
199 \end{lstlisting} |
204 \end{lstlisting} |