Q1)What is Anonymous functions in Scala?
Ans:An anonymous function is an expression that evaluates to a function;
the function is defined without giving it a name.
Example:
(x:Int) => x*x*x
Tuesday, March 6, 2018
Higher-order function
Q1)what is higher-order function in Scala?
Ans:Functions which take other functions as parameters or return them as results are called higher-order functions.
Exmple:
Normal function
def square(x: Int): Int = x * x
High-order function:
def sumSquares(a: Int, b: Int): Int = if (a > b) 0 else square(a) + sumSquares(a + 1, b)
Ans:Functions which take other functions as parameters or return them as results are called higher-order functions.
Exmple:
Normal function
def square(x: Int): Int = x * x
High-order function:
def sumSquares(a: Int, b: Int): Int = if (a > b) 0 else square(a) + sumSquares(a + 1, b)
Monday, March 5, 2018
Tail Recursion in Scala
Q1)What is tail-recursion and why in this scenario Scala is Better then Java?
Ans:
If the last action of a function is a call to another (possibly the same)
function, only a single stack frame is needed for both functions. Such calls are called
“tail calls”. In principle, tail calls can always re-use the stack frame of the calling
function. However, some run-time environments (such as the Java VM) lack the
primitives to make stack frame re-use for tail calls efficient. A production quality
Scala implementation is therefore only required to re-use the stack frame of a di-
rectly tail-recursive function whose last action is a call to itself. Other tail calls might
be optimized also, but one should not rely on this across implementations.
Example:
def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
gcd(14, 21)
→if (21 == 0) 14 else gcd(21, 14 % 21)
→if (false) 14 else gcd(21, 14 % 21)
→gcd(21, 14 % 21)
→gcd(21, 14)
→if (14 == 0) 21 else gcd(14, 21 % 14)
→ → gcd(14, 21 % 14)
→gcd(14, 7)
→if (7 == 0) 14 else gcd(7, 14 % 7)
→ → gcd(7, 14 % 7)
→gcd(7, 0)
→if (0 == 0) 7 else gcd(0, 7 % 0)
→ → 7
Ans:
If the last action of a function is a call to another (possibly the same)
function, only a single stack frame is needed for both functions. Such calls are called
“tail calls”. In principle, tail calls can always re-use the stack frame of the calling
function. However, some run-time environments (such as the Java VM) lack the
primitives to make stack frame re-use for tail calls efficient. A production quality
Scala implementation is therefore only required to re-use the stack frame of a di-
rectly tail-recursive function whose last action is a call to itself. Other tail calls might
be optimized also, but one should not rely on this across implementations.
Example:
def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
gcd(14, 21)
→if (21 == 0) 14 else gcd(21, 14 % 21)
→if (false) 14 else gcd(21, 14 % 21)
→gcd(21, 14 % 21)
→gcd(21, 14)
→if (14 == 0) 21 else gcd(14, 21 % 14)
→ → gcd(14, 21 % 14)
→gcd(14, 7)
→if (7 == 0) 14 else gcd(7, 14 % 7)
→ → gcd(7, 14 % 7)
→gcd(7, 0)
→if (0 == 0) 7 else gcd(0, 7 % 0)
→ → 7
Call-by-Value and Call-by-name in Scala
Q1)What is Call-by-Value and Call-by-name in Scala?
Ans:
Define One Function
def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
Call-by-value
Call-by-value has the advantage that it avoids repeated evaluation of arguments.
Call-by-value is usually more efficient than call-by-name.
example:
sumOfSquares(5, 3+3)
→ sumOfSquares(5, 6)
→ square(5) + square(6)
→ 5 * 5 + square(6)
→ 25 + square(6)
→ 25 + 6 * 6
→ 25 + 36
→ 61
Call-by-name
Call-by-name has the advantage that it avoids evaluation of arguments when the
parameter is not used at all by the function.
example:
sumOfSquares(5, 3+3)
→ square(5) + square(3+3)
→ 5 * 5 + square(3+3)
→ 25 + square(3+3)
→ 25 + (3+3) * (3+3)
→ 25 + 6 * (3+3)
→ 25 + 6 * 6
→ 25 + 36
→ 61
Note:
Scala uses call-by-value by default, but it switches to call-by-name evaluation if the
parameter type is preceded by =>
Ans:
Define One Function
def sumOfSquares(x: Double, y: Double) = square(x) + square(y)
Call-by-value
Call-by-value has the advantage that it avoids repeated evaluation of arguments.
Call-by-value is usually more efficient than call-by-name.
example:
sumOfSquares(5, 3+3)
→ sumOfSquares(5, 6)
→ square(5) + square(6)
→ 5 * 5 + square(6)
→ 25 + square(6)
→ 25 + 6 * 6
→ 25 + 36
→ 61
Call-by-name
Call-by-name has the advantage that it avoids evaluation of arguments when the
parameter is not used at all by the function.
example:
sumOfSquares(5, 3+3)
→ square(5) + square(3+3)
→ 5 * 5 + square(3+3)
→ 25 + square(3+3)
→ 25 + (3+3) * (3+3)
→ 25 + 6 * (3+3)
→ 25 + 6 * 6
→ 25 + 36
→ 61
Note:
Scala uses call-by-value by default, but it switches to call-by-name evaluation if the
parameter type is preceded by =>
Friday, March 2, 2018
Small step towards learning Scala
Q1)To get Out put as mentioned below pattern.
Code Snippet:
(1 to 5).map("*"*_).foreach(println(_))
OutPut:
*
**
***
****
*****
Q2)To get all the even number between two number i.e 1 to 10
Code Snippet:
(1 to 10).filter(_%2==0).foreach(println(_))
Output:
2
4
6
8
10
Q3)One way to find factorial of a number?
Code Snippet:
(1 to 5).reduceLeft(_*_)
or def factorial(n: Int):Int = if(n==0) 1 else n*factorial(n-1)
Output:
120
Q4)Split the string_> sort the string by length,Print in sorted length all word?
input: var s= "Scala supports both object-oriented and functional"
Code Snippet:
s.split(" ").sortWith(_.length<_.length).foreach(println(_))
<--> OutPut:
and
both
Scala
supports
functional
object-oriented
Q5)How to call function inside function,let's suppose we pass one list and one max value and get all the value less then max value from the list? Input:
List:List(9,5,1,4,7,3,8)
Max Vaue:8
Code Snippet:
object Nest extends App{
| def fun1(l:List[Int],max:Int)={
| def fun2(m:List[Int]):List[Int]=
| if(m.isEmpty) m
| else if(m.head
| else fun2(m.tail)
| fun2(l)
| }
| println(fun1(List(9,5,1,4,7,3,8),5))
| }
Invocation:<-->
Nest.fun1(List(9,5,1,4,7,3,8),8)
Output:
List[Int] = List(5, 1, 4, 7, 3)
Code Snippet:
(1 to 5).map("*"*_).foreach(println(_))
OutPut:
*
**
***
****
*****
Q2)To get all the even number between two number i.e 1 to 10
Code Snippet:
(1 to 10).filter(_%2==0).foreach(println(_))
Output:
2
4
6
8
10
Q3)One way to find factorial of a number?
Code Snippet:
(1 to 5).reduceLeft(_*_)
or def factorial(n: Int):Int = if(n==0) 1 else n*factorial(n-1)
Output:
120
Q4)Split the string_> sort the string by length,Print in sorted length all word?
input: var s= "Scala supports both object-oriented and functional"
Code Snippet:
s.split(" ").sortWith(_.length<_.length).foreach(println(_))
<--> OutPut:
and
both
Scala
supports
functional
object-oriented
Q5)How to call function inside function,let's suppose we pass one list and one max value and get all the value less then max value from the list? Input:
List:List(9,5,1,4,7,3,8)
Max Vaue:8
Code Snippet:
object Nest extends App{
| def fun1(l:List[Int],max:Int)={
| def fun2(m:List[Int]):List[Int]=
| if(m.isEmpty) m
| else if(m.head
| fun2(l)
| }
| println(fun1(List(9,5,1,4,7,3,8),5))
| }
Invocation:<-->
Nest.fun1(List(9,5,1,4,7,3,8),8)
Output:
List[Int] = List(5, 1, 4, 7, 3)
Subscribe to:
Posts (Atom)