Wednesday, 10 October 2012

Functional programming in Scala



Main programming paradigms

Imperative programming
Functional programming
Logical programming

Functional programming
·         
           Wider sense: Programming with focusing on functions.
·        Restricted sense: Programming without using mutable variables, assignments,loops and other imperative control statements.

Some functional programming languages:
In the wider sense :

·        Lisp,Scheme,Racket,Clojure
·        SML,Ocaml,F#
·        Haskell (full language)
·        Scala
·        Smalltalk,Ruby(!)

In the restricted sense:

·        Pure Lisp,XSLT,XPath,XQuery,FP
·        Haskell(without I/O Monad or UnsafePerformIO)

REPL(Read-Eval-Print)

An interactive shell or REPL allows one to write expressions and responds with their value. Scala REPL can be started by typing

          >scala
          For Example:
                 
                    >scala 34+65
                    res0: Int = 99
                    >scala def radius = 10
                    radius: Int
                    scala>def pi = 3.14159
                    scala>radius * pi
                    res1: Double = 31.4159

Order of evaluation of arithmetic operation:
                  
                    (2 * pi) * radius
                    (2 * 3.14159) * radius
                    6.28318 * 10
                    62.8318

Definitions can have parameters:

scala> def square(x: Double) =  x * x
square: (Double)Double
scala> def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
sumOfSquares: (Double,Double)Double

Expression evaluation strategies Call-by-name and Call-by-value:

Both strategies reduce to the same final values as long as
·        The reduced expression consist of pure functions and
·        Both evaluations terminate

Consider an example:
 
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

Scala normally uses call-by-value. But if the type of function starts with => it uses call-by-name.

Conditional Expressions

Scala has a conditional expression if-else. It looks likes a if-else in Java, but is used for expressions not statements.
          Example:
                   def abs(x: Int) = if (x >= 0) x else -x

Boolean Expressions
           
         Boolean expressions b can be composed of
                   true false               // Constants
                   !b                          // Negation
                   b && b                 // Conjunction
                   b || b                     // Disjunction
          and of the usual comparison operations:
                   e <= e, e >= e, e < e, e > e, e == e, e != e
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.


Semicolons
          
          In Scala, semicolons at the end of lines are in most cases optional
          You could write
                   val x = 1; but not necessary.
On the other hand, if there are more  than one statements on a line, they need to be separated by semicolons:
          val y = x + 1; y * y
More references

  • ·         Structure and Interpretation of Computer Programs. Harold Abelson and Gerald J. Sussman. 2nd edition. MIT Press 1996 Download

  • ·         Programming in Scala. Martin Odersky, Lex Spoon, and Bill Venners. 2nd edition. Artima 2010.

  • ·         Scala for the Impatient. Cay Horstmann Download


No comments:

Post a Comment