handouts/scala-ho.tex
changeset 237 370c0647a9bf
parent 236 34e901c529ce
child 238 527fdb90fffe
equal deleted inserted replaced
236:34e901c529ce 237:370c0647a9bf
   602 def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
   602 def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
   603 \end{lstlisting}
   603 \end{lstlisting}
   604 
   604 
   605 
   605 
   606 \noindent
   606 \noindent
   607 Since this function returns a pair of integers, its type
   607 Since this function returns a pair of integers, its 
   608 needs to be \code{(Int, Int)}. 
   608 return type needs to be \code{(Int, Int)}. Incidentally,
       
   609 this is also the input type of this function. Notice it takes
       
   610 \emph{two} arguments, namely \code{m} and \code{n}, both
       
   611 of which are integers. They are ``packaged'' in a pair.
       
   612 Consequently the complete type of \code{quo_rem} is
       
   613 
       
   614 \begin{lstlisting}[language=Scala, numbers=none]
       
   615 (Int, Int) => (Int, Int)
       
   616 \end{lstlisting}
   609 
   617 
   610 Another special type-constructor is for functions, written
   618 Another special type-constructor is for functions, written
   611 as the arrow \code{=>}. For example, the type \code{Int =>
   619 as the arrow \code{=>}. For example, the type 
   612 String} is for a function that takes an integer as argument
   620 \code{Int => String} is for a function that takes an integer as argument
   613 and produces a string. A function of this type is for instance
   621 and produces a string. A function of this type is for instance
   614 
   622 
   615 
   623 
   616 \begin{lstlisting}[language=Scala,numbers=none]
   624 \begin{lstlisting}[language=Scala,numbers=none]
   617 def mk_string(n: Int) : String = n match {
   625 def mk_string(n: Int) : String = n match {
   897 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
   905 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
   898 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
   906 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
   899 \item \url{https://www.youtube.com/user/ShadowofCatron}
   907 \item \url{https://www.youtube.com/user/ShadowofCatron}
   900 \end{itemize}
   908 \end{itemize}
   901 
   909 
       
   910 \noindent There is also a course at Coursera on Functional
       
   911 Programming Principles in Scala by Martin Odersky, the main
       
   912 developer of the Scala language.
       
   913 
   902 While I am quite enthusiastic about Scala, I am also happy to
   914 While I am quite enthusiastic about Scala, I am also happy to
   903 admit that it has more than its fair share of faults. The
   915 admit that it has more than its fair share of faults. The
   904 problem seen earlier of having to give an explicit type to
   916 problem seen earlier of having to give an explicit type to
   905 \code{toSet}, but not \code{toList} is one of them. There are
   917 \code{toSet}, but not \code{toList} is one of them. There are
   906 also many ``deep'' ideas about types in Scala, which even to
   918 also many ``deep'' ideas about types in Scala, which even to
   934 it. In the coursework you can use any programming language you
   946 it. In the coursework you can use any programming language you
   935 like. If you want to use Scala for this, then be my guest; if
   947 like. If you want to use Scala for this, then be my guest; if
   936 you do not want, stick with the language you are most familiar
   948 you do not want, stick with the language you are most familiar
   937 with.
   949 with.
   938 
   950 
       
   951 
       
   952 
   939 \end{document}
   953 \end{document}
   940 
   954 
   941 %%% Local Variables: 
   955 %%% Local Variables: 
   942 %%% mode: latex
   956 %%% mode: latex
   943 %%% TeX-master: t
   957 %%% TeX-master: t