handouts/pep-ho.tex
changeset 333 24bc76d97db2
parent 329 8a34b2ebc8cc
child 334 841727e27252
equal deleted inserted replaced
332:703c7e42bf46 333:24bc76d97db2
    39 %https://www.metalevel.at/prolog/optimization
    39 %https://www.metalevel.at/prolog/optimization
    40 
    40 
    41 % nice example for map and reduce using Harry potter characters
    41 % nice example for map and reduce using Harry potter characters
    42 % https://www.matthewgerstman.com/map-filter-reduce/
    42 % https://www.matthewgerstman.com/map-filter-reduce/
    43 
    43 
       
    44 % interesting talk about differences in Java and Scala
       
    45 % Goto'19 conference ; about differences in type-system
       
    46 % https://www.youtube.com/watch?v=e6n-Ci8V2CM
    44 
    47 
    45 % Timing
    48 % Timing
    46 %
    49 %
    47 % xs.map(x => (x, xs.count(_==x)))
    50 % xs.map(x => (x, xs.count(_==x)))
    48 %
    51 %
   205 
   208 
   206 \subsection*{Why Functional Programming?}
   209 \subsection*{Why Functional Programming?}
   207 
   210 
   208 Before we go on, let me explain a bit more why we want to inflict upon
   211 Before we go on, let me explain a bit more why we want to inflict upon
   209 you another programming language. You hopefully have mastered Java and
   212 you another programming language. You hopefully have mastered Java and
   210 C++\ldots{}the world should be your oyster, no? Well, this is not as
   213 C++\ldots{}the world should be your oyster, no? Well, matters are not as
   211 simple as one might wish. We do require Scala in PEP, but actually we
   214 simple as one might wish. We do require Scala in PEP, but actually we do
   212 do not religiously care whether you learn Scala---after all it is just
   215 not religiously care whether you learn Scala---after all it is just a
   213 a programming language (albeit a nifty one IMHO). What we do care
   216 programming language (albeit a nifty one IMHO). What we do care about is
   214 about is that you learn about \textit{functional programming}. Scala
   217 that you learn about \textit{functional programming}. Scala is just the
   215 is just the vehicle for that. Still, you need to learn Scala well
   218 vehicle for that. Still, you need to learn Scala well enough to get good
   216 enough to get good marks in PEP, but functional programming could
   219 marks in PEP, but functional programming could perhaps equally be taught
   217 equally be taught with Haskell, F\#, SML, Ocaml, Kotlin, Clojure,
   220 with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many
   218 Scheme, Elm and many other functional programming languages.
   221 other functional programming languages. 
   219 %Your
   222 
   220 %friendly lecturer just happens to like Scala
   223 %Your friendly lecturer just
   221 %and the Department agreed that it is a good idea to inflict Scala upon
   224 %happens to like Scala and the Department agreed that it is a good idea
   222 %you.
   225 %to inflict Scala upon you.
   223 
   226 
   224 Very likely writing programs in a functional programming language is
   227 Very likely writing programs in a functional programming language is
   225 quite different from what you are  used to in your study so far. It
   228 quite different from what you are  used to in your study so far. It
   226 might even be totally alien to you. The reason is that functional
   229 might even be totally alien to you. The reason is that functional
   227 programming seems to go against the core principles of
   230 programming seems to go against the core principles of
   263 is that this clock-speed has not much increased over the past decade and
   266 is that this clock-speed has not much increased over the past decade and
   264 no dramatic increases are predicted for any time soon. So you are a bit
   267 no dramatic increases are predicted for any time soon. So you are a bit
   265 stuck. This is unlike previous generations of developers who could rely
   268 stuck. This is unlike previous generations of developers who could rely
   266 upon the fact that approximately every 2 years their code would run
   269 upon the fact that approximately every 2 years their code would run
   267 twice as fast  because the clock-speed of their CPUs got twice as fast.
   270 twice as fast  because the clock-speed of their CPUs got twice as fast.
       
   271 
   268 Unfortunately this does not happen any more nowadays. To get you out of
   272 Unfortunately this does not happen any more nowadays. To get you out of
   269 this dreadful situation, CPU producers pile more and more cores into
   273 this dreadful situation, CPU producers pile more and more cores into
   270 CPUs in order to make them more powerful and potentially make software
   274 CPUs in order to make them more powerful and potentially make software
   271 faster. The task for you as developer is to take somehow advantage of
   275 faster. The task for you as developer is to take somehow advantage of
   272 these cores by running as much of your code as possible in parallel on
   276 these cores by running as much of your code as possible in parallel on
   273 as many cores you have available (typically 4 in modern laptops and
   277 as many cores you have available (typically 4 or more in modern laptops
   274 sometimes much more on high-end machines). In this situation
   278 and sometimes much more on high-end machines). In this situation
   275 \textit{mutable} variables like \texttt{i} above are evil, or at least a
   279 \textit{mutable} variables like \texttt{i} in the C-code above are evil,
   276 major nuisance: Because if you want to distribute some of the
   280 or at least a major nuisance: Because if you want to distribute some of
   277 loop-iterations over the cores that are currently idle in your system,
   281 the loop-iterations over several cores that are currently idle in your
   278 you need to be extremely careful about who can read and overwrite the
   282 system, you need to be extremely careful about who can read and
   279 variable \texttt{i}.\footnote{If you are of the mistaken belief that
   283 overwrite the variable \texttt{i}.\footnote{If you are of the mistaken
   280 nothing nasty can happen to \texttt{i} inside the \texttt{for}-loop,
   284 belief that nothing nasty can happen to \texttt{i} inside the
   281 then you need to go back over the C++ material.} Especially the writing
   285 \texttt{for}-loop, then you need to go back over the C++ material.}
   282 operation is critical because you do not want that conflicting writes
   286 Especially the writing operation is critical because you do not want
   283 mess about with \texttt{i}. Take my word: an untold amount of misery has
   287 that conflicting writes mess about with \texttt{i}. Take my word: an
   284 arisen from this problem. The catch is that if you try to solve this
   288 untold amount of misery has arisen from this problem. The catch is that
   285 problem in C/C++ or Java, and be as defensive as possible about reads
   289 if you try to solve this problem in C/C++ or Java, and be as defensive
   286 and writes to \texttt{i}, then you need to synchronise access to it. The
   290 as possible about reads and writes to \texttt{i}, then you need to
   287 result is that very often your program waits more than it runs, thereby
   291 synchronise access to it. The result is that very often your program
   288 defeating the point of trying to run the program in parallel in the
   292 waits more than it runs, thereby defeating the point of trying to run
   289 first place. If you are less defensive, then usually all hell breaks
   293 the program in parallel in the first place. If you are less defensive,
   290 loose by seemingly obtaining random results. And forget the idea of
   294 then usually all hell breaks loose by seemingly obtaining random
   291 being able to debug such code.
   295 results. And forget the idea of being able to debug such code.
   292 
   296 
   293 The central idea of functional programming is to eliminate any state
   297 The central idea of functional programming is to eliminate any state
   294 from programs---or at least from the ``interesting bits'' of the
   298 from programs---or at least from the ``interesting bits'' of the
   295 programs. Because then it is easy to parallelise the resulting
   299 programs. Because then it is easy to parallelise the resulting
   296 programs: if you do not have any state, then once created, all memory
   300 programs: if you do not have any state, then once created, all memory
  1630 language is lately developing at lightening speed (in comparison to the past) 
  1634 language is lately developing at lightening speed (in comparison to the past) 
  1631 taking on many
  1635 taking on many
  1632 features of Scala and other languages, and it seems even it introduces
  1636 features of Scala and other languages, and it seems even it introduces
  1633 new features on its own.
  1637 new features on its own.
  1634 
  1638 
       
  1639 
       
  1640 Scala is deep: After many years, I still continue to learn new technique
       
  1641 for writing more elegant code.
       
  1642 
  1635 %So all in all, Scala might not be a great teaching language,
  1643 %So all in all, Scala might not be a great teaching language,
  1636 %but I hope this is mitigated by the fact that I never require
  1644 %but I hope this is mitigated by the fact that I never require
  1637 %you to write any Scala code. You only need to be able to read
  1645 %you to write any Scala code. You only need to be able to read
  1638 %it. In the coursework you can use any programming language you
  1646 %it. In the coursework you can use any programming language you
  1639 %like. If you want to use Scala for this, then be my guest; if
  1647 %like. If you want to use Scala for this, then be my guest; if