Open in CoCalc with one click!

Scala 2.11 in SMC

In [4]:
println("hi")
hi
In [5]:
def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2))) println(filter(nums, modN(3)))
List(2, 4, 6, 8) List(3, 6)
defined function filter defined function modN nums: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
In [6]:
def dup[T](x: T, n: Int): List[T] = { if (n == 0) Nil else x :: dup(x, n - 1) } println(dup[Int](3, 4)) println(dup("three", 3))
List(3, 3, 3, 3) List(three, three, three)
defined function dup
In [ ]: