progs/lecture1.scala
changeset 123 556cd74cbba9
parent 51 0e60e6c24b99
child 124 c45d3cd9a749
--- a/progs/lecture1.scala	Wed May 31 09:26:08 2017 +0100
+++ b/progs/lecture1.scala	Thu Nov 02 14:47:55 2017 +0000
@@ -2,11 +2,15 @@
 //=================
 
 // Value assignments
-// (variable names should be lower case)
-//======================================
+// (their names should be lower case)
+//===================================
 
 val x = 42
 val y = 3 + 4
+val z = x / y
+
+
+// (you cannot reassign values: z = 9 will give an error)
 
 
 // Collections
@@ -66,7 +70,7 @@
 List(1,2,3,4).sum
 List(1,2,3,4).take(2).sum
 List(1,2,3,4).drop(2).sum
-List(1,2,3,4,3).indexOf(3)
+List(1,2,3,4,3)indexOf(3)
 
 "1,2,3,4,5".split(",").mkString("\n")
 "1,2,3,4,5".split(",3,").mkString("\n")
@@ -144,7 +148,9 @@
 // Function Definitions
 //======================
 
-def square(x: Int): Int = x * x
+def incr(x: Int) : Int = x + 1
+def double(x: Int) : Int = x + x
+def square(x: Int) : Int = x * x
 
 square(6)
 
@@ -159,8 +165,8 @@
 
 
 
-// If control structure
-//======================
+// If-Conditionals
+//=================
 
 def fact(n: Int): Int = 
   if (n == 0) 1 else n * fact(n - 1)
@@ -190,12 +196,18 @@
 
 //gcd - Euclid's algorithm
 
-def gcd(a: Int, b: Int): Int =
+def gcd(a: Int, b: Int) : Int =
   if (b == 0) a else gcd(b, a % b)
 
 gcd(48, 18)
 
 
+def power(x: Int, n: Int) : Int =
+  if (n == 0) 1  else x * power(x, n - 1) 
+
+power(5, 5)
+
+
 // String Interpolations
 //=======================
 
@@ -206,7 +218,7 @@
 
 
 
-def gcd_db(a: Int, b: Int): Int = {
+def gcd_db(a: Int, b: Int) : Int = {
   println(s"Function called with ${a} and ${b}.")
   if (b == 0) a else gcd_db(b, a % b)
 }
@@ -285,13 +297,13 @@
 import io.Source
 
 // obtaining a webpage
-val url = """http://www.inf.kcl.ac.uk/staff/urbanc/""" 
+val url = """https://nms.kcl.ac.uk/christian.urban/""" 
 Source.fromURL(url)("ISO-8859-1").mkString
 
 
 // function for looking up stockmarket data 
 def price_lookup(symbol: String): String = {
-  val url = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
+  val url = "https://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=snl1"
   Source.fromURL(url).mkString.drop(1).dropRight(2)
 }
 
@@ -302,13 +314,14 @@
 val companies = 
   List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
 
-for (s <- companies.par) println(price_lookup(s))
+for (s <- companies) println(price_lookup(s))
 
 
-// A Web Crawler
+// A Web Crawler 
 //===============
 //
-// the idea is to look for dead links
+// the idea is to look for dead links using the
+// regular expression "https?://[^"]*"
 
 import io.Source
 import scala.util.matching.Regex
@@ -340,7 +353,7 @@
 }
 
 // some starting URLs for the crawler
-val startURL = """http://www.inf.kcl.ac.uk/staff/urbanc"""
+val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
 //val startURL = """http://www.inf.kcl.ac.uk/staff/mcburney"""
 
 crawl(startURL, 2)
@@ -350,19 +363,67 @@
 // Further Information
 //=====================
 
-// Scala download
+// The Scala home page and general information is at
 //
 //  http://www.scala-lang.org
-
-// Eclipse for Scala
+//	http://docs.scala-lang.org
+//
+//
+// It should be fairly easy to install the Scala binary and
+// run Scala on the commandline. There are also at least 
+// four IDEs you can use with Scala:
+//
+//  (0) Some general information for setting up IDEs
+//	    with Scala support can be found at
+//
+//         http://docs.scala-lang.org/getting-started.html 
+//
+//  (1) Eclipse for Scala (one big bundle)
+//
+//         http://scala-ide.org/download/sdk.html
+//  
+//  (2) IntelliJ (needs additional Plugins)
+//
+//         https://www.jetbrains.com/idea/
+//		   http://docs.scala-lang.org/getting-started-intellij-track/getting-started-with-scala-in-intellij.html	  
+//
+//  (3) Sublime (not free, but unlimited trial period; 
+//	    needs SublimeREPL plugin)
+//
+//         https://www.sublimetext.com
+//
+//  (4) Emacs (old-fashioned, but reliable)
+//
+//         https://www.gnu.org/software/emacs/
 //
-//  http://scala-ide.org/download/sdk.html
+//      I use the old scala-tool support for Emacs distributed at
+//
+//         https://github.com/scala/scala-tool-support/tree/master/tool-support/emacs 
+//
+//      but there is also support for the newer Ensime Scala Mode
+//
+//         http://ensime.org/editors/emacs/scala-mode/   
+//   
+// There is also Scala support in the Atom editor, but my
+// experience is mixed. People also use Scala with Vim and Jedit.
+//
+// All of the IDEs above support a REPL for Scala. Some of them have
+// the very nifty feature of a Scala Worksheet -- you just save your
+// file and it will be automatically evaluated and the result pasted
+// into your file. However, this way of writing Scala code never worked
+// for me. I just use the REPL.
+//
+//
+// Scala Library Docs
+//
+//  http://www.scala-lang.org/api/current/
+//
+// Scala Tutorials
+//
+//  http://docs.scala-lang.org/tutorials/
+//
+// There are also a massive number of Scala tutorials on youtube
+// and there are tons of books and free material.
+//
 
 
-// library docs
-//
-//  http://www.scala-lang.org/api/current/
-
-// tutorials
-//
-//  http://docs.scala-lang.org/tutorials/