added assignment 5 ex 1
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/Assignment5/HOFOnLists.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/Assignment5/HOFOnLists.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| def lengthStrings(strings: List[String]): List[Int] = { | ||||
|   strings map (s => s.length) | ||||
| } | ||||
|  | ||||
| def dup[T](elem: T, n: Int): List[T] = { | ||||
|   (1 to n).toList map (_ => elem) | ||||
| } | ||||
|  | ||||
| def dot(list1: List[Int], list2: List[Int]): List[Int] = { | ||||
|   list1 zip list2 map (p => p._1 * p._2) | ||||
| } | ||||
|  | ||||
| assert(lengthStrings(List("How","long","are","we?")) == List(3, 4, 3, 3)) | ||||
|  | ||||
| assert(dup("foo", 5) == List("foo", "foo", "foo", "foo", "foo")) | ||||
| assert(dup(List(1,2,3), 2) == List(List(1,2,3), List(1,2,3))) | ||||
|  | ||||
| assert(dot(List(1,2,3), List(2,4,3)) == List(2,8,9)) | ||||
		Reference in New Issue
	
	Block a user