| changeset 321 | f6cb539e0ffb | 
| parent 320 | 90aed247c8cf | 
| child 323 | 93b6c16dded8 | 
| 320:90aed247c8cf | 321:f6cb539e0ffb | 
|---|---|
45 // visits pages potentially more than once  | 
45 // visits pages potentially more than once  | 
46 def crawl(url: String, n: Int) : Unit = {  | 
46 def crawl(url: String, n: Int) : Unit = {  | 
47 if (n == 0) ()  | 
47 if (n == 0) ()  | 
48 else {  | 
48 else {  | 
49 println(s" Visiting: $n $url")  | 
49 println(s" Visiting: $n $url")  | 
50 for (u <- get_all_URLs(get_page(url))) crawl(u, n - 1)  | 
50 val page = get_page(url)  | 
51 for (u <- get_all_URLs(page)) crawl(u, n - 1)  | 
|
51 }  | 
52 }  | 
52 }  | 
53 }  | 
53  | 
54  | 
54 // some starting URLs for the crawler  | 
55 // some starting URLs for the crawler  | 
55 val startURL = """https://nms.kcl.ac.uk/christian.urban/"""  | 
56 val startURL = """https://nms.kcl.ac.uk/christian.urban/"""  | 
119  | 
120  | 
120 // checks whether a jump tour exists at all  | 
121 // checks whether a jump tour exists at all  | 
121  | 
122  | 
122 def search(xs: List[Int]) : Boolean = xs match {  | 
123 def search(xs: List[Int]) : Boolean = xs match {  | 
123 case Nil => true  | 
124 case Nil => true  | 
124 case (x::xs) =>  | 
125 case x::xs =>  | 
125 if (xs.length < x) true else moves(xs, x).exists(search(_))  | 
126 if (xs.length < x) true  | 
127 else moves(xs, x).exists(search(_))  | 
|
126 }  | 
128 }  | 
127  | 
129  | 
128  | 
130  | 
129 search(List(5,3,2,5,1,1))  | 
131 search(List(5,3,2,5,1,1))  | 
130 search(List(3,5,1,0,0,0,1))  | 
132 search(List(3,5,1,0,0,0,1))  | 
136 search(List(1))  | 
138 search(List(1))  | 
137 search(List(5,1,1))  | 
139 search(List(5,1,1))  | 
138 search(List(3,5,1,0,0,0,0,0,0,0,0,1))  | 
140 search(List(3,5,1,0,0,0,0,0,0,0,0,1))  | 
139  | 
141  | 
140 // generates *all* jump tours  | 
142 // generates *all* jump tours  | 
141 // if we are only interested in the shortes one, we could  | 
143 // if we are only interested in the shortest one, we could  | 
142 // shortcircut the calculation and only return List(x) in  | 
144 // shortcircut the calculation and only return List(x) in  | 
143 // case where xs.length < x, because no tour can be shorter  | 
145 // case where xs.length < x, because no tour can be shorter  | 
144 // than 1  | 
146 // than 1  | 
145 //  | 
147 //  | 
146  | 
148  | 
147 def jumps(xs: List[Int]) : List[List[Int]] = xs match {  | 
149 def jumps(xs: List[Int]) : List[List[Int]] = xs match {  | 
148 case Nil => Nil  | 
150 case Nil => Nil  | 
149 case (x::xs) => {  | 
151 case x::xs => {  | 
150 val children = moves(xs, x)  | 
152 val children = moves(xs, x)  | 
151 val results = children.map(cs => jumps(cs).map(x :: _)).flatten  | 
153 val results = children.map(cs => jumps(cs).map(x :: _)).flatten  | 
152 if (xs.length < x) List(x)::results else results  | 
154 if (xs.length < x) List(x)::results else results  | 
153 }  | 
155 }  | 
154 }  | 
156 }  | 
178  | 
180  | 
179 // User-defined Datatypes  | 
181 // User-defined Datatypes  | 
180 //========================  | 
182 //========================  | 
181  | 
183  | 
182  | 
184  | 
183 abstract class Colour  | 
185 sealed abstract class Colour  | 
184 case object Red extends Colour  | 
186 case object Red extends Colour  | 
185 case object Green extends Colour  | 
187 case object Green extends Colour  | 
186 case object Blue extends Colour  | 
188 case object Blue extends Colour  | 
187  | 
189  | 
188  | 
190  | 
194  | 
196  | 
195 fav_colour(Green)  | 
197 fav_colour(Green)  | 
196  | 
198  | 
197 // ... a tiny bit more useful: Roman Numerals  | 
199 // ... a tiny bit more useful: Roman Numerals  | 
198  | 
200  | 
199 abstract class RomanDigit  | 
201 sealed abstract class RomanDigit  | 
200 case object I extends RomanDigit  | 
202 case object I extends RomanDigit  | 
201 case object V extends RomanDigit  | 
203 case object V extends RomanDigit  | 
202 case object X extends RomanDigit  | 
204 case object X extends RomanDigit  | 
203 case object L extends RomanDigit  | 
205 case object L extends RomanDigit  | 
204 case object C extends RomanDigit  | 
206 case object C extends RomanDigit  | 
267 // User-defined Datatypes and Pattern Matching  | 
269 // User-defined Datatypes and Pattern Matching  | 
268 //=============================================  | 
270 //=============================================  | 
269  | 
271  | 
270 // trees  | 
272 // trees  | 
271  | 
273  | 
272 abstract class Exp  | 
274 sealed abstract class Exp  | 
273 case class N(n: Int) extends Exp // for numbers  | 
275 case class N(n: Int) extends Exp // for numbers  | 
274 case class Plus(e1: Exp, e2: Exp) extends Exp  | 
276 case class Plus(e1: Exp, e2: Exp) extends Exp  | 
275 case class Times(e1: Exp, e2: Exp) extends Exp  | 
277 case class Times(e1: Exp, e2: Exp) extends Exp  | 
276  | 
278  | 
277 def string(e: Exp) : String = e match {  | 
279 def string(e: Exp) : String = e match {  | 
312 println(string(e2))  | 
314 println(string(e2))  | 
313 println(string(simp(e2)))  | 
315 println(string(simp(e2)))  | 
314  | 
316  | 
315  | 
317  | 
316 // Tokens and Reverse Polish Notation  | 
318 // Tokens and Reverse Polish Notation  | 
317 abstract class Token  | 
319 sealed abstract class Token  | 
318 case class T(n: Int) extends Token  | 
320 case class T(n: Int) extends Token  | 
319 case object PL extends Token  | 
321 case object PL extends Token  | 
320 case object TI extends Token  | 
322 case object TI extends Token  | 
321  | 
323  | 
322 def rp(e: Exp) : List[Token] = e match {  | 
324 def rp(e: Exp) : List[Token] = e match {  |