Question

In: Computer Science

In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some...

In Scala, what is First-order Function, Recursion, and Immutability? How to write them? Please provide some examples and details to explain it. Also, what will be the benefits of using them when writing codes.

Solutions

Expert Solution

Answers:-

Scala

  • Scala defined as a the programming language that are used by both functional programming and object oriented programming which gives the support for creating applications.
  • Companies which uses scala are as follows:
  1. Twitter
  2. LinkedIn
  3. Netflix
  4. Airbnb
  5. Tumblr
  • In Scala functions is a complete object which are used for assigning variables and these variables passed to some another function and these function, which returns value to the function are known as first order function and also function which are used to define as a member of various object is known as methods.
  • In Scala Recursion is used to divided or breaking the complex problems into sub smaller parts i.e function calling itself .
  • Scala supports immutable because in scala there are two distinguish collection i.e mutable means you can add, remove, change the values and second is Immutable means value does not changed.

Following are the steps write code in scala are as follows:

  1. Install Scala in your system
  2. Check JDK is available on your system or not, if not install JDK because scala needs JDK to create .class file which is in byte code.
  3. JVM is also used in scala to execute byte code.
  • Example of Simple Scala Program

object ScalaExam

{

def main(args:Array[String])

{

println "HELLO SCALA"

}

}

After code steps

  • File name: Save this file as ScalaExam.scala
  • Command for compilation : scala ScalaExam.scala
  • Command to execute compiled code: scala ScalaExam
  • Output: HELLO SCALA

Related Solutions

Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of...
2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of a word is defined as the sum of the values of the individual letters of the word with value of "a" being 1, "b" being 2 etc. Upper and lower case letters have the same value. You can assume that the word consists of just letters. Here is the signature of the function: def wordValue(s: String): Int = ??? For example, wordValue("Attitude") = 100....
Please write code in SCALA Thanks in advance Use curried representation and write system which convert...
Please write code in SCALA Thanks in advance Use curried representation and write system which convert pressure units. Input: pressure in atm (atmosphere) Outputs: PSI, bar, torr 1 atm = 14.6956 psi = 760 torr = 101325 Pa = 1.01325 bar.
-> > Use recursion to write a C++ function for determining if a strings has more...
-> > Use recursion to write a C++ function for determining if a strings has more vowels than consonants. Please include the pseudo code so that I can understand better with simple English as much as possible.
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is a list of ints. smallest_sum should return the length of the smallest combinations of L that add up to A if there are no possible combinations then float("inf") is returned. Examples smallest_sum(5, [2, 3]) == 2 smallest_sum(313, [7, 24, 42]) == 10 smallest_sum(13, [8, 3, 9, 6]) == float(‘‘inf’’) No Loops and map/reduce functions are allowed
Recursion Part //1. Write out the output for eaching of following: /* Consider this function. void...
Recursion Part //1. Write out the output for eaching of following: /* Consider this function. void myMethod( int counter) { if(counter == 0) return; else { System.out.println(""+counter); myMethod(--counter); return; } } This recursion is not infinite, assuming the method is passed a positive integer value. What will the output be? b. Consider this method: void myMethod( int counter) { if(counter == 0) return; else { System.out.println("hello" + counter); myMethod(--counter); System.out.println(""+counter); return; } } If the method is called with the...
In your own words please answer the following, or some of them: 5. How was the...
In your own words please answer the following, or some of them: 5. How was the income tax formula for individuals changed for 2018 by the Tax Cuts and Jobs Act? 6. Why are some deductions called “above the line” deductions and others called “below the line” deductions? What is the “line”? 7. For tax purposes, why is the married filing jointly tax status generally preferable to the married filing separately filing status? Why might a married taxpayer prefer not...
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks...
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks in advance Write /the/ generator /function/ genAccum(seq, fn), which takes in an iterable and a function fn and yields each accumulated value from applying fn to the running total and the next element. (Note: to convert an iterable into an iterator you use iter(iterable)) ''' >>> add = lambda x, y: x + y >>> mul = lambda x, y: x * y >>>...
How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT