0
votes

Background

I tried to answer a question "what is function?" and wonder if I actually know what it is. Please help to understand what a "function" is in Scala. It may sound non-sense debate but please be patient to help.

Questions

1. What is function

A "function" is a "computation/operation" to be "applied" to a single "argument" to generate an "value". If there are multiple argument, then it can be converted into ()()()... which is called currying.

  • w is a name
  • b is a binding of a function object to a name
  • c is computation
  • a is application
  • g is argument on which apply an compuation
val w   =  ( )  => { }
    ^   ^   ^   ^   ^
    |   |   |   |   |
   (n) (b) (g) (a) (c)

Is it OK to say these?

Also if this is to apply a computation to an argument:

() => { }

Then it actually should be in the opposite direction?

() <= { }
or 
{ } => ()

2. Decomposition of definition

Is this correct understanding of what "def f (x:Unit):Unit = {}" is?

//--------------------------------------------------------------------------------
// The function literal says:
// 1. Define a "function" (ignore "method" here).
// 2. Bind the function to a name f. 
// 3. It is to be applied to an "argument" of type Unit.
// 4. Bind the argument to a name x.
// 5. E-valuation, or appliation of the function to an argument generates an "value" of type Unit.
// 6. After evaluation, substitute it with the "value".
//--------------------------------------------------------------------------------
def f (x:Unit):Unit = {}

3. Evaluation / Application

Is "evaluation" the same with "application of a function to an argument and yield an value"? When I read lambda calculas, word "application" is used, but I think "evaluation" is also used.

Unit

//--------------------------------------------------------------------------------
// The literal says:
// 1. Apply the function f
// 2. on the "argument" enclosed between '(' and ')', which is Unit.
// 3. and yield Unit as the "value" evaluated.
//--------------------------------------------------------------------------------
def f (x:Unit):Unit = {} 
f()

Is it the same with this? If so is "Unit" an object?

f(Unit)  // No error in Scala worksheet

What is causing the error "Too many arguments" for Unit as argument below?

// Define a function that applies on an argument x of type Unit to generate Unit
() => {}     // res0: () => Unit = <function0>
(() => {})   // res1: () => Unit = <function0>

// Application
(() => {})()

/* Error: Too many arguments */
(() => {})(Unit)

4. Referential transparency

Please advise if this is correct.

Using "def g (x:String): Unit = println(x)" as an example, "referential transparency" means that g(x) can be always substituted with its result and it will not break any.

If

g("foo")

can be always replaced with

Unit

then it is referentially transparent. However, it is not the case here for g. Hence g is not a referentially transparent function, hence it is not "pure" function. Random is also not pure.

{ scala.util.Random.nextInt } // res0: Int = -487407277

In Scala, function can be pure or side-effective. There is no way to tell by just having look at a function. Or is there a way to mark as such, or validate if it is pure or not?

5. Method is not a function

A method cannot be a first class object to be passed around but it can be by converting it to a function.

def g (x:String): Unit = println(x)
g("foo")

val _g = g _
_g("foo")

Why method cannot be a first class object? If a method is an object, what will happen or what will break?

Scala compiler does clever inferences or complehentions, then if it can be converted into an object with _, why Scala does not make it a firt class object?

6. What is { ... }?

Update:

"=> T" is call by name passing expression to be evaluated inside the function, hence has nothing to do with "{...}" specifically. {...} is a block expression. Hence all below is invalid.

It looks "{...}" is the same with "=> T".

def fill[T](n: Int)(elem: => T)

Array.fill[Int](3)({ scala.util.Random.nextInt })

{...} in itself yield an value without taking any argument.

{ scala.util.Random.nextInt }    // res0: Int = 951666328
{ 1 }                            // res1: Int = 1

Does it mean "application" is an independent first class object, or the Scala compiler is clever enough to understand it is an abbreviation of:

() => { scala.util.Random.nextInt }
or 
val next = (x:Int) => { scala.util.Random.nextInt(x) }

If so, "=> T" is actually "() => T"?

1
Scala is not the only language with functions, nor did the idea of functions originate from programming languages such as scala. If you want to understand what functions are, don't do it in the context of a programming language because that will cause you to start to confuse the language syntax with what functions really are. Functions deal with input and output and have a concept of a domain (the acceptable inputs) and range (the possible outputs). All the fancy syntax scala adds to this is just for readabilitysmac89
A method can not be a first class member because of "its definition". In OOP, objects can have value members and behaviour members. Methods are supposed to be the these behaviour members of an object which define a certain behaviour, they can not be values themselves.sarveshseri
This has too many questions and they are not about specific problems that need solving. Please see the guidelines on how to ask a question.Tim
But a simplistic definition would be: f(x: A): B = y means a mapping between an object of type A (which we will call x) to an object of type B. And the function is just an expression (called y in this case) that may use the value of x.Luis Miguel Mejía Suárez

1 Answers

0
votes

In Scala function is an implementation of one of traits from Function1 to Function22 depending on input parameters amount. For your particular example w is a shorthand for anonfunW:

val w = () => {}
val anonfunW = new Function1[Unit, Unit] {
    def apply(x: Unit): Unit = ()
  }