FunProg-Scala/src/Assignment5/HOFOnLists.sc

18 lines
525 B
Scala

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))