Scala uses recursive thinking and programming

14.1 Basic Introduction

Scala ornament runs on the Java Virtual Machine, so it has the following characteristics

1) Easy implementation and rich Java class libraries interconnection

2) It supports both object-oriented programming and functional programming.

3) The program it writes is as concise as a dynamic language, but in fact it is a strictly static language.

14.2 Scala advocates functional programming (recursive thinking)

Start with the programming paradigm:

Object-Oriented Programming has no intention of being the biggest winner of all programming paradigms

2) But in fact, object-oriented programming is not a programming paradigm in the strict sense, which can be divided into Imperative Programming, Functional Programming and Logic Programming. Object-oriented programming is only a cross-product of the above paradigms, and it inherits more of the genes of imperative programming.

3) In traditional language design, only imperative programming is emphasized, that is, the programmer tells the computer what to do. Recursion, on the other hand, tells the computer what to do through a clever function definition. Therefore, in the program of using imperative programming thinking, most programs now adopt the programming method, the probability of recursive mirror is very small, while in functional programming, recursive way can be seen everywhere.

14.3 Application Case 1

Loops in Scala do not recommend while and do...while, but recursion

14.3.1 Case Requirements

Calculate the sum of 1-100

14.3.2 Conventional Solutions

import java.text.SimpleDateFormat
import java.util.Date

object boke_demo01 {
  def main(args: Array[String]): Unit = {

    //Traditional method completes 1-100 summation task
    val now: Date = new Date()
    val dateFormat: SimpleDateFormat =
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val date = dateFormat.format(now)

    println("date=" + date) //Output time
    var res = BigInt(0)
    var num = BigInt(1)
    var maxVal = BigInt(100l) //BigInt(99999999l) [Large Test Efficiency]
    while (num <= maxVal) {
      res += num
      num += 1
    }
    println("res=" + res)
    //Re-output time
    val now2: Date = new Date()
    val date2 = dateFormat.format(now2)
    println("date2=" + date2) //Output time

  }
}

14.3.3 Using Functional Programming - Recursion

The important idea of functional programming is to try not to have any additional impact. The above case code does not conform to the idea of functional programming. Here we use functional programming to solve it (Scala advocates).

Case demonstration

import java.text.SimpleDateFormat
import java.util.Date

object boke_demo01 {
  def main(args: Array[String]): Unit = {

    // Recursive solution
    //Traditional method completes 1-100 summation task
    val now: Date = new Date()
    val dateFormat: SimpleDateFormat =
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val date = dateFormat.format(now)
    println("date=" + date) //Output time

    def mx(num: BigInt, sum: BigInt): BigInt = {
      if (num <= 100l) return mx(num + 1, sum + num)
      else return sum
    }

    //test
    var num = BigInt(1)
    var sum = BigInt(0)
    var res = mx(num, sum)
    println("res=" + res)

    //Re-output time
    val now2: Date = new Date()
    val date2 = dateFormat.format(now2)
    println("date2=" + date2) //Output time

  }
}

14.4 Application Case 2

Maximum

  def max(xs: List[Int]): Int = {
    if (xs.isEmpty)
      throw new java.util.NoSuchElementException
    if (xs.size == 1) xs.head
    else if (xs.head > max(xs.tail)) xs.head else max(xs.tail)
  }

14.5 Use Functional Programming - String Flip

def reverse(xs: String): String =
    if (xs.length == 1) xs else reverse(xs.tail) + xs.head

14.6 Use recursion-factorization

def factorial(n: Int): Int =
    if (n == 0) 1 else n * factorial(n - 1)

Keywords: Scala Programming Java

Added by billmasters on Tue, 21 May 2019 21:43:22 +0300