Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A variable used in its own definition?

An infinite stream:

val ones: Stream[Int] = Stream.cons(1, ones)

How is it possible for a value to be used in its own declaration? It seems this should produce a compiler error, yet it works.

like image 435
axiopisty Avatar asked Nov 05 '15 03:11

axiopisty


People also ask

What is your own definition of a variable?

A variable is a quantity that may change within the context of a mathematical problem or experiment. Typically, we use a single letter to represent a variable. The letters x, y, and z are common generic symbols used for variables.

What does it mean to use a variable?

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

What are the 3 types of variables?

There are three main variables: independent variable, dependent variable and controlled variables. Example: a car going down different surfaces.

What is another word for variables?

Noun. Plural for an element, feature, or factor that is liable to vary or change. factors. parameters. consideration.


3 Answers

It's not always a recursive definition. This actually works and produces 1:

val a : Int = a + 1
println(a)

variable a is created when you type val a: Int, so you can use it in the definition. Int is initialized to 0 by default. A class will be null.

As @Chris pointed out, Stream accepts => Stream[A] so a bit another rules are applied, but I wanted to explain general case. The idea is still the same, but the variable is passed by-name, so this makes the computation recursive. Given that it is passed by name, it is executed lazily. Stream computes each element one-by-one, so it calls ones each time it needs next element, resulting in the same element being produces once again. This works:

val ones: Stream[Int] = Stream.cons(1, ones)
println((ones take 10).toList) // List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

Though you can make infinite stream easier: Stream.continually(1) Update As @SethTisue pointed out in the comments Stream.continually and Stream.cons are two completely different approaches, with very different results, because cons takes A when continually takes =>A, which means that continually recomputes each time the element and stores it in the memory, when cons can avoid storing it n times unless you convert it to the other structure like List. You should use continually only if you need to generate different values. See @SethTisue comment for details and examples.

But notice that you are required to specify the type, the same as with recursive functions.

And you can make the first example recursive:

lazy val b: Int = b + 1
println(b)

This will stackoverflow.

like image 199
Archeg Avatar answered Oct 21 '22 13:10

Archeg


Look at the signature of Stream.cons.apply:

apply[A](hd: A, tl: ⇒ Stream[A]): Cons[A]

The on the second parameter indicates that it has call-by-name semantics. Therefore your expression Stream.cons(1, ones) is not strictly evaluated; the argument ones does not need to be computed prior to being passed as an argument for tl.

like image 31
Chris Martin Avatar answered Oct 21 '22 11:10

Chris Martin


The reason this does not produce a compiler error is because both Stream.cons and Cons are non-strict and lazily evaluate their second parameter.

ones can be used in it's own definition because the object cons has an apply method defined like this:

/** A stream consisting of a given first element and remaining elements
 *  @param hd   The first element of the result stream
 *  @param tl   The remaining elements of the result stream
 */
def apply[A](hd: A, tl: => Stream[A]) = new Cons(hd, tl)

And Cons is defined like this:

final class Cons[+A](hd: A, tl: => Stream[A]) extends Stream[A]

Notice that it's second parameter tl is passed by name (=> Stream[A]) rather than by value. In other words, the parameter tl is not evaluated until it is used in the function.

One advantage to using this technique is that you can compose complex expressions that may be only partially evaluated.

like image 3
axiopisty Avatar answered Oct 21 '22 11:10

axiopisty