91 |
91 |
92 \subsection*{VS Code and Scala} |
92 \subsection*{VS Code and Scala} |
93 |
93 |
94 I found a convenient IDE for writing Scala programs is Microsoft's |
94 I found a convenient IDE for writing Scala programs is Microsoft's |
95 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
95 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
96 obviously Windows.\footnote{unlike \emph{Microsoft Visual Studio}---note |
96 obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note |
97 the minuscule difference in the name---which is a heavy-duty, |
97 the minuscule difference in the name---which is a heavy-duty, |
98 Windows-only IDE\ldots{}jeez, with all their money could they not come |
98 Windows-only IDE\ldots{}jeez, with all their money could they not have come |
99 up with a completely different name for a complete different project? |
99 up with a completely different name for a complete different project? |
100 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
100 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual |
101 Studio Code is considered as a source code editor. Anybody knows the what the |
101 Studio Code is considered to be a \emph{source code editor}. Anybody knows what the |
102 difference is?} It can be downloaded for free from |
102 difference is?} It can be downloaded for free from |
103 |
103 |
104 \begin{quote} |
104 \begin{quote} |
105 \url{https://code.visualstudio.com} |
105 \url{https://code.visualstudio.com} |
106 \end{quote} |
106 \end{quote} |
144 |
144 |
145 \noindent |
145 \noindent |
146 But you should be careful if you use them for your coursework: they |
146 But you should be careful if you use them for your coursework: they |
147 are meant to play around, not really for serious work. |
147 are meant to play around, not really for serious work. |
148 |
148 |
149 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
149 As one might expect, Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
150 A ready-made Scala bundle for Eclipse is available from |
150 A ready-made Scala bundle for Eclipse is available from |
151 |
151 |
152 \begin{quote} |
152 \begin{quote} |
153 \url{http://scala-ide.org/download/sdk.html} |
153 \url{http://scala-ide.org/download/sdk.html} |
154 \end{quote} |
154 \end{quote} |
155 |
155 |
156 \noindent |
156 \noindent |
157 Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, |
157 Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, |
158 I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs |
158 I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs |
159 seem to make your life harder, rather than easier, for the small |
159 seem to make your life harder, rather than easier, for the small |
160 programs we will write in this module. They are really meant to be used |
160 programs that we will write in this module. They are really meant to be used |
161 when you have a million-lines codebase, rather than our |
161 when you have a million-lines codebase than with our small |
162 ``toy-programs'' we will write in PEP\ldots{}for example why on earth am I required to create a |
162 ``toy-programs''\ldots{}for example why on earth am I required to create a |
163 completely new project with several subdirectories when I just want to |
163 completely new project with several subdirectories when I just want to |
164 try out 20-lines of Scala code? Your mileage may vary though. ;o) |
164 try out 20-lines of Scala code? Your mileage may vary though. ;o) |
165 |
165 |
166 \subsection*{Why Functional Programming?} |
166 \subsection*{Why Functional Programming?} |
167 |
167 |
168 Before we go on, let me explain a bit more why we want to inflict upon |
168 Before we go on, let me explain a bit more why we want to inflict upon |
169 you another programming language. You hopefully have mastered Java and |
169 you another programming language. You hopefully have mastered Java and |
170 C++\ldots{}the world should be your oyster, no? Well, it is not that |
170 C++\ldots{}the world should be your oyster, no? Well, this is not as |
171 easy. We do require Scala in PEP, but actually we do not religiously |
171 simple as one might wish. We do require Scala in PEP, but actually we |
172 care whether you learn Scala---after all it is just a programming |
172 do not religiously care whether you learn Scala---after all it is just |
173 language (albeit a nifty one IMHO). What we do care about is that you |
173 a programming language (albeit a nifty one IMHO). What we do care |
174 learn about \textit{functional programming}. Scala is just the vehicle |
174 about is that you learn about \textit{functional programming}. Scala |
175 for that. Still, you need to learn Scala well enough to get good marks |
175 is just the vehicle for that. Still, you need to learn Scala well |
176 in PEP, but functional programming could equally be taught with Haskell, |
176 enough to get good marks in PEP, but functional programming could |
177 F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other functional |
177 equally be taught with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, |
178 programming languages. %Your friendly lecturer just happens to like Scala |
178 Scheme, Elm and many other functional programming languages. |
|
179 %Your |
|
180 %friendly lecturer just happens to like Scala |
179 %and the Department agreed that it is a good idea to inflict Scala upon |
181 %and the Department agreed that it is a good idea to inflict Scala upon |
180 %you. |
182 %you. |
181 |
183 |
182 Very likely writing programs in a functional programming language is |
184 Very likely writing programs in a functional programming language is |
183 quite different from what you are used to in your study so far. It |
185 quite different from what you are used to in your study so far. It |
186 \textit{imperative programming} (which is what you do in Java and C++ |
188 \textit{imperative programming} (which is what you do in Java and C++ |
187 for example). The main idea of imperative programming is that you have |
189 for example). The main idea of imperative programming is that you have |
188 some form of \emph{state} in your program and you continuously change this |
190 some form of \emph{state} in your program and you continuously change this |
189 state by issuing some commands---for example for updating a field in an |
191 state by issuing some commands---for example for updating a field in an |
190 array or for adding one to a variable and so on. The classic |
192 array or for adding one to a variable and so on. The classic |
191 example for this style of programming are \texttt{for}-loops in C: |
193 example for this style of programming is \texttt{for}-loops in C/C++. Consider |
|
194 the snippet: |
192 |
195 |
193 \begin{lstlisting}[language=C,numbers=none] |
196 \begin{lstlisting}[language=C,numbers=none] |
194 for (int i = 10; i < 20; i++) { |
197 for (int i = 10; i < 20; i++) { |
195 //...Do something interesting with i... |
198 //...do something with i... |
196 } |
199 } |
197 \end{lstlisting} |
200 \end{lstlisting} |
198 |
201 |
199 \noindent Here the integer variable \texttt{i} embodies the state, which |
202 \noindent Here the integer variable \texttt{i} embodies the state, which |
200 is first set to \texttt{10} and then increased by one in each |
203 is first set to \texttt{10} and then increased by one in each |
201 loop-iteration until it reaches \texttt{20} at which point the loop |
204 loop-iteration until it reaches \texttt{20} at which point the loop |
202 exits. When this code is compiled and actually runs, there will be some |
205 exits. When this code is compiled and actually runs, there will be some |
203 dedicated space reserved for \texttt{i} in memory. This space of |
206 dedicated space reserved for \texttt{i} in memory. This space of |
204 typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10} |
207 typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10} |
205 at the beginning, and then the content will be overwritten with some |
208 at the beginning, and then the content will be overwritten with |
206 new content in every iteration. The main point here is that this kind of |
209 new content in every iteration. The main point here is that this kind of |
207 updating, or manipulating, memory is 25.806\ldots or \textbf{THE ROOT OF |
210 updating, or manipulating, memory is 25.806\ldots or \textbf{THE ROOT OF |
208 ALL EVIL}!! |
211 ALL EVIL}!! |
209 |
212 |
210 \begin{center} |
213 \begin{center} |