39 //============= |
39 //============= |
40 |
40 |
41 List(1,2,3,1) |
41 List(1,2,3,1) |
42 Set(1,2,3,1) |
42 Set(1,2,3,1) |
43 |
43 |
|
44 // picking an element in a list |
|
45 val lst = List(1, 2, 3, 1) |
|
46 |
|
47 lst(0) |
|
48 lst(2) |
|
49 |
|
50 // head and tail |
|
51 lst.head |
|
52 lst.tail |
|
53 |
|
54 // some alterative syntax for lists |
|
55 |
|
56 Nil // empty list |
|
57 |
|
58 1 :: 2 :: 3 :: Nil |
|
59 List(1, 2, 3) ::: List(4, 5, 6) |
|
60 |
|
61 // also |
|
62 List(1, 2, 3) ++ List(3, 6, 5) |
|
63 Set(1, 2, 3) ++ Set(3, 6, 5) |
|
64 |
44 // ranges |
65 // ranges |
45 1 to 10 |
66 1 to 10 |
46 (1 to 10).toList |
67 (1 to 10).toList |
47 (1 to 10).toList.toString |
68 (1 to 10).toList.toString |
48 |
69 |
49 (1 until 10).toList |
70 (1 until 10).toList |
50 |
71 |
51 // picking an element in a list |
|
52 val lst = List(1, 2, 3, 1) |
|
53 lst(0) |
|
54 lst(2) |
|
55 |
|
56 // some alterative syntax for lists |
|
57 |
|
58 1 :: 2 :: 3 :: Nil |
|
59 List(1, 2, 3) ::: List(4, 5, 6) |
|
60 |
|
61 // also |
|
62 List(1, 2, 3) ++ List(4, 5, 6) |
|
63 |
|
64 |
72 |
65 // Equality in Scala is structural |
73 // Equality in Scala is structural |
66 //================================= |
74 //================================= |
67 val a = "Dave" |
75 val a = "Dave2" |
68 val b = "Dave" |
76 val b = "Dave" |
69 |
77 |
70 if (a == b) println("Equal") else println("Unequal") |
78 if (a == b) println("Equal") else println("Unequal") |
71 |
79 |
72 Set(1,2,3) == Set(3,1,2) |
80 Set(1,2,3) == Set(3,1,2) |
111 |
119 |
112 List(1,2,3,1).toString |
120 List(1,2,3,1).toString |
113 List(1,2,3,1).toSet |
121 List(1,2,3,1).toSet |
114 |
122 |
115 "hello".toList |
123 "hello".toList |
116 "hello".toList.tail |
124 "hello".toSet |
117 |
125 |
118 |
126 |
119 1.toDouble |
127 1.toDouble |
120 |
128 |
121 1L // a long |
129 1 // an Int |
122 1F // a float |
130 1L // a Long |
123 |
131 1F // a Float |
124 // useful list methods |
132 1D // a Double |
|
133 |
|
134 // useful list methods on lists |
|
135 //============================== |
125 |
136 |
126 List(1,2,3,4).length |
137 List(1,2,3,4).length |
127 List(1,2,3,4).reverse |
138 List(1,2,3,4).reverse |
128 List(1,2,3,4).max |
139 List(1,2,3,4).max |
129 List(1,2,3,4).min |
140 List(1,2,3,4).min |
193 |
204 |
194 |
205 |
195 // Function Definitions |
206 // Function Definitions |
196 //====================== |
207 //====================== |
197 |
208 |
198 def foo(s: String) : String = { |
|
199 val tmp = s ++ s ++ s |
|
200 val res = s ++ s |
|
201 res |
|
202 } |
|
203 |
|
204 |
|
205 foo("test") |
|
206 |
209 |
207 def incr(x: Int) : Int = x + 1 |
210 def incr(x: Int) : Int = x + 1 |
208 def double(x: Int) : Int = x + x |
211 def double(x: Int) : Int = x + x |
209 def square(x: Int) : Int = x * x |
212 def square(x: Int) : Int = x * x |
210 |
213 |
211 def str(x: Int) : String = x.toString |
214 def str(x: Int) : String = x.toString |
|
215 |
212 |
216 |
213 incr(3) |
217 incr(3) |
214 double(4) |
218 double(4) |
215 square(6) |
219 square(6) |
216 str(3) |
220 str(3) |
278 def power(x: Int, n: Int) : Int = |
272 def power(x: Int, n: Int) : Int = |
279 if (n == 0) 1 else x * power(x, n - 1) |
273 if (n == 0) 1 else x * power(x, n - 1) |
280 |
274 |
281 power(5, 5) |
275 power(5, 5) |
282 |
276 |
|
277 // BTW: no returns!! |
|
278 // "last" line (expression) in a function determines the |
|
279 // result |
283 |
280 |
284 |
281 |
285 // For-Comprehensions (not For-Loops) |
282 // For-Comprehensions (not For-Loops) |
286 //==================================== |
283 //==================================== |
287 |
284 |