--- a/cws/cw02.tex Thu Nov 10 11:41:50 2016 +0000
+++ b/cws/cw02.tex Thu Nov 10 16:10:39 2016 +0000
@@ -14,7 +14,7 @@
-\section*{Coursework 2 (Knight's Tour)}
+\section*{Coursework 7 (Scala, Knight's Tour)}
This coursework is worth XXX\% and is due on 21 November at 16:00. You
are asked to implement a Scala program that solves the \textit{knight's
--- a/progs/lecture1.scala Thu Nov 10 11:41:50 2016 +0000
+++ b/progs/lecture1.scala Thu Nov 10 16:10:39 2016 +0000
@@ -68,8 +68,8 @@
List(1,2,3,4).drop(2).sum
List(1,2,3,4,3).indexOf(3)
-"1,2,3,4,5".split(",").toList
-
+"1,2,3,4,5".split(",").mkString("\n")
+"1,2,3,4,5".split(",3,").mkString("\n")
// Types
//=======
@@ -93,8 +93,9 @@
// Smart Strings
//===============
-println(">\n<")
+println(">\n\n<")
println(""">\n<""")
+println("""">\n<"""")
/* in Java
val lyrics = "Baa, Baa, Black Sheep \n" +
@@ -148,12 +149,26 @@
square(6)
+
+// The general scheme for a function: you have to give a type
+// to each argument and a return type of the function
+//
+// def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
+// body
+// }
+
+
+
// If control structure
//======================
def fact(n: Int): Int =
if (n == 0) 1 else n * fact(n - 1)
+
+fact(5)
+fact(150)
+
/* boolean operators
== equals
@@ -239,19 +254,18 @@
-// with only a side-effect (no list is produced)
+// with only a side-effect (no list is produced),
// has no "yield"
for (n <- (1 to 10)) println(n)
-// concurrency (ONLY WORKS IN 2.11.8)
+// concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12.0)
for (n <- (1 to 10)) println(n)
for (n <- (1 to 10).par) println(n)
-
-// for testing time
+// for measuring time
def time_needed[T](i: Int, code: => T) = {
val start = System.nanoTime()
for (j <- 1 to i) code
@@ -270,11 +284,12 @@
import io.Source
+// obtaining a webpage
val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
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"
Source.fromURL(url).mkString.drop(1).dropRight(2)
@@ -292,6 +307,8 @@
// A Web Crawler
//===============
+//
+// the idea is to look for dead links
import io.Source
import scala.util.matching.Regex
@@ -330,26 +347,6 @@
-
-// Adding your own methods to Strings
-//====================================
-
-// imagine you want to implement an additional
-// method to strings, like
-//
-// "HAL".increment
-//
-// you can avoid ugly fudges, like a MyString
-// class by using implicit conversions
-
-
-implicit class MyString(s: String) {
- def increment = for (c <- s) yield (c + 1).toChar
-}
-
-"HAL".increment
-
-
// Further Information
//=====================
--- a/progs/lecture2.scala Thu Nov 10 11:41:50 2016 +0000
+++ b/progs/lecture2.scala Thu Nov 10 16:10:39 2016 +0000
@@ -1,2 +1,23 @@
// sudoku
// some none
+
+
+
+
+// Implicits
+//===========
+//
+// for example adding your own methods to Strings:
+// imagine you want to increment strings, like
+//
+// "HAL".increment
+//
+// you can avoid ugly fudges, like a MyString, by
+// using implicit conversions
+
+
+implicit class MyString(s: String) {
+ def increment = for (c <- s) yield (c + 1).toChar
+}
+
+"HAL".increment