3

For the following question: http://pastie.org/4825115, here is my code: http://pastie.org/private/n22zohyshn2ymqrbrb3g

def randList(len: Int, n: Int): List[Int] = len match {
  case 0 => List()
  case len => scala.util.Random.nextInt(n) :: randList(len-1, n)
}

but I don't know why randList is called a closure.

3 Answers3

19

According to my understanding randList is definitely not a closure (Wikipedia seems to agree) , since - in the snippet of code you provided - it only depends on local variables (parameters are also considered local variables). Considering the body of randList, there is no so-called free variable, i.e., a variable that does not get its value from the current lexical scope, where the latter is the method body itself. len and n are both variables of the current lexical scope since they are both parameters of the enclosing definition of randList.

Consider this example:

var n = 10
val f = (x: Int) => x + n

println(f(1)) // 11

n = 20
println(f(1)) // 21

The function f is a closure because it does not only depend on its parameters, but also on a variable that is declared outside of its own lexical scope (namely n).

The Wikipedia article mentions that a closure is defined by a function together with a lexical scope that declares the free arguments. The next example illustrates this:

// n == 20
// f as above

def foo(g: Int => Int) = {
  val n = 100
  g(1)
}

println(foo(f)) // 21

The result of foo(f) is still 21 although foo defines its own local variable n, and one might assume that f now uses this n. However, the closure f is coupled to the lexical scope that surrounds its declarations, which is where the value of n is take from when f is evaluated.

Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
  • "The Wikipedia article mentions that a closure is defined by a function together with a lexical scope that declares the free arguments." But that means we really dont have to do any changes to function definition. Even if function does not use variables declared in its lexical scope, it still gets access to them (just that it doesn use them) right? So, in reality, there are no two different things: functions and closures, right? Or I am missing something? – Mahesha999 Mar 28 '18 at 07:08
0

I agree with @Malte as randList is not dependent on any variable declared outside the function. So this function is not a closure.

R Pidugu
  • 490
  • 5
  • 8
-1

To my understanding, a closure is a function that can reference state in another function. Look this thread for more details: What is a 'Closure'?.

In this problem, as randList doesn't reference any outer variables, it's not a closure...

Community
  • 1
  • 1
chyx
  • 501
  • 2
  • 10
  • Could you give me example of currying this function in Scala? Something like: def rand5 = randList(5) or val rand5 = randList(5) gives me an error in Scala. – Minh-Triet Pham Tran Sep 27 '12 at 06:34
  • 1
    If you want to implement currying, you should define `randList` as `randList(len: Int)(n: Int)`. – chyx Sep 27 '12 at 06:54