Functional Programming is becoming increasingly popular because it offers an attractive method for exploiting parallelism for multicore and cloud computing. As the name indicates functional programming focuses on the functions. A functional programming language is one which does not have mutable variables, assignments, loops, and other imperative control structures. Some of the important functional programming languages are
- Lisp, Scheme, Racket, Clojure
- SML, Ocaml, F#
- Haskell (full language)
- Scala
- Smalltalk, Ruby (!)
REPL(Read-Eval-Print)
An interactive shell or REPL lets one write expressions and responds with their value. Scala REPL can be started by typing
>scala
For example;
>scala 23+34
57
>scala def length = 10
length: Int
>scala def breadth = 20
breadth: Int
>scala length*breadth
200
Parameters and Return type in def
Definition may consist of parameters. Sometimes return types are also specified in the definition. For example
scala> def sum(x:Int, y:Int) = x+y
sum: (x: Int, y: Int)Int
scala> sum(2,3)
5
Function parameters come with their type, which is given after a colon. For example;
def isDevisible(x:Double, y:Int): Boolean =
Evaluation strategies: Call-by-value & Call-by-name
Call by name and call by value are two types of evaluation strategies employed in functional programming. In the case of call by value, the interpreter reduces function arguments to values before rewriting the function application. But for call by name evaluation, one could alternatively apply the function to unreduced arguments.
Both strategies reduce to the same final values as long as
sumOfSquares(2, 3+4) sumOfSquare(2, 3+4)
sumOfSquare(2,7) square(2) + square(3+4)
square(2) + square(7) 2 * 2 + square(3+4)
2 * 2 + square(7) 4 + square(3+4)
4 + square(7) 4 + (3+4) * (3+4)
4 + 7*7 4 + 7 * (3+4)
4+ 49 4 + 7 * 7
53 4 + 49
53
Evaluation of the function sumOfSquares which computes the sum of the squares of the arguments is explained above. The left one corresponds to call by value method and the other illustrates the call by name method.
Scala normally uses call-by-value, but if the type of a function parameter starts with => it uses call-by-name.
Conditional Expressions
Conditional expression if-else is used in scala to choose between alternatives.
Eg;
def isPositive(x: Int) = if (x >= 0) true else false
x >= 0 is a predicate, of type Boolean. Boolean expressions b can be composed of
Constants (true, false),Negation(!), Conjunction(&&), Disjunction(||) and of the usual comparison operations.
Blocks in Scala
It is a good discipline in functional programming to arrange the code into separate blocks so that it avoids the name space pollution and becomes more readable. Every block of code will have a start and end braces. Consider the below given example;
val x = 3
val y = 1
def sum(x: Int , y: Int) = x + y
val res = {
val y =sum(x,1)
y * y
}
In scala, for a single line of code, use of semicolon is optional. But when there are more than one statements in a line, they need to be separated by semicolons.
eg: val x = 23; x + 1
One problem with semicolon convention is to write codes that span several lines. There are two alternatives to solve this problem.
An interactive shell or REPL lets one write expressions and responds with their value. Scala REPL can be started by typing
>scala
For example;
>scala 23+34
57
>scala def length = 10
length: Int
>scala def breadth = 20
breadth: Int
>scala length*breadth
200
Parameters and Return type in def
Definition may consist of parameters. Sometimes return types are also specified in the definition. For example
scala> def sum(x:Int, y:Int) = x+y
sum: (x: Int, y: Int)Int
scala> sum(2,3)
5
Function parameters come with their type, which is given after a colon. For example;
def isDevisible(x:Double, y:Int): Boolean =
Evaluation strategies: Call-by-value & Call-by-name
Call by name and call by value are two types of evaluation strategies employed in functional programming. In the case of call by value, the interpreter reduces function arguments to values before rewriting the function application. But for call by name evaluation, one could alternatively apply the function to unreduced arguments.
Both strategies reduce to the same final values as long as
- the reduced expression consists of pure functions, and
- both evaluations terminate.
sumOfSquares(2, 3+4) sumOfSquare(2, 3+4)
sumOfSquare(2,7) square(2) + square(3+4)
square(2) + square(7) 2 * 2 + square(3+4)
2 * 2 + square(7) 4 + square(3+4)
4 + square(7) 4 + (3+4) * (3+4)
4 + 7*7 4 + 7 * (3+4)
4+ 49 4 + 7 * 7
53 4 + 49
53
Evaluation of the function sumOfSquares which computes the sum of the squares of the arguments is explained above. The left one corresponds to call by value method and the other illustrates the call by name method.
Scala normally uses call-by-value, but if the type of a function parameter starts with => it uses call-by-name.
Conditional Expressions
Conditional expression if-else is used in scala to choose between alternatives.
Eg;
def isPositive(x: Int) = if (x >= 0) true else false
x >= 0 is a predicate, of type Boolean. Boolean expressions b can be composed of
Constants (true, false),Negation(!), Conjunction(&&), Disjunction(||) and of the usual comparison operations.
Blocks in Scala
It is a good discipline in functional programming to arrange the code into separate blocks so that it avoids the name space pollution and becomes more readable. Every block of code will have a start and end braces. Consider the below given example;
val x = 3
val y = 1
def sum(x: Int , y: Int) = x + y
val res = {
val y =sum(x,1)
y * y
}
- The last element of a block is an expression that defines its value.
- Blocks are themselves expressions; a block may appear everywhere an expression can.
- The definitions inside a block are only visible from within the block.
- The definitions inside a block shadow definitions of the same names outside the block.
In scala, for a single line of code, use of semicolon is optional. But when there are more than one statements in a line, they need to be separated by semicolons.
eg: val x = 23; x + 1
One problem with semicolon convention is to write codes that span several lines. There are two alternatives to solve this problem.
- Write the multi-line expression in paranthesis.
- Write the operator of the expression in the first line.
These are some basics of principles of functional programming in Scala. Some good references of the topic are given below:
1. Structure and Interpretation of Computer Programs. Harold Abelson and Gerald J. Sussman. 2nd edition. MIT Press 1996. Download
2. Programming in Scala. Martin Odersky, Lex Spoon, and Bill Venners. 2nd edition. Artima 2010.
3.Scala for the Impatient. Cay Horstmann Download
Comments
Post a Comment