handouts/scala-ho.tex
changeset 245 a5fade10c207
parent 242 35104ee14f87
child 247 84b4bc6e8554
equal deleted inserted replaced
244:771042ac7c3f 245:a5fade10c207
    86 string. Once you are more familiar with the functional
    86 string. Once you are more familiar with the functional
    87 programming-style, you will know what the difference is
    87 programming-style, you will know what the difference is
    88 between a function that returns a result, like addition, and a
    88 between a function that returns a result, like addition, and a
    89 function that causes a side-effect, like \code{print}. We
    89 function that causes a side-effect, like \code{print}. We
    90 shall come back to this point later, but if you are curious
    90 shall come back to this point later, but if you are curious
    91 now, the latter kind of functions always has as return type
    91 now, the latter kind of functions always has \code{Unit} as
    92 \code{Unit}.
    92 return type.
    93 
    93 
    94 If you want to write a stand-alone app in Scala, you can
    94 If you want to write a stand-alone app in Scala, you can
    95 implement an object that is an instance of \code{App}, say
    95 implement an object that is an instance of \code{App}, say
    96 
    96 
    97 \begin{lstlisting}[numbers=none]
    97 \begin{lstlisting}[numbers=none]
   147 inner nodes---sequence, alternative and star---and three kinds
   147 inner nodes---sequence, alternative and star---and three kinds
   148 of leave nodes---null, empty and character). If you are
   148 of leave nodes---null, empty and character). If you are
   149 familiar with Java, it might be an instructive exercise to
   149 familiar with Java, it might be an instructive exercise to
   150 define this kind of inductive datatypes in Java\footnote{Happy
   150 define this kind of inductive datatypes in Java\footnote{Happy
   151 programming! \Smiley} and then compare it with how it can be
   151 programming! \Smiley} and then compare it with how it can be
   152 defined in Scala.
   152 implemented in Scala.
   153 
   153 
   154 Implementing the regular expressions from above in Scala is
   154 Implementing the regular expressions from above in Scala is
   155 actually very simple: It first requires an \emph{abstract
   155 actually very simple: It first requires an \emph{abstract
   156 class}, say, \code{Rexp}. This will act as the type for
   156 class}, say, \code{Rexp}. This will act as the type for
   157 regular expressions. Second, it requires a case for each
   157 regular expressions. Second, it requires a case for each
   358 Line 4 indicates that we do not care what the pattern looks
   358 Line 4 indicates that we do not care what the pattern looks
   359 like. Thus this case acts like a default case whenever the
   359 like. Thus this case acts like a default case whenever the
   360 cases above did not match. Cases are always tried out from top
   360 cases above did not match. Cases are always tried out from top
   361 to bottom.
   361 to bottom.
   362  
   362  
   363 \subsection*{Loops, or the Absence of}
   363 \subsection*{Loops, or the Absence thereof}
   364 
   364 
   365 Coming from Java or C, you might be surprised that Scala does
   365 Coming from Java or C, you might be surprised that Scala does
   366 not really have loops. It has instead, what is in functional
   366 not really have loops. It has instead, what is in functional
   367 programming called, \emph{maps}. To illustrate how they work,
   367 programming called, \emph{maps}. To illustrate how they work,
   368 lets assume you have a list of numbers from 1 to 8 and want to
   368 lets assume you have a list of numbers from 1 to 8 and want to