Interesting Scala language: think in a recursive way

Source: Internet
Author: User
Tags filter empty function definition sort

When I was learning computer programming, I think most people would experience the same as the author, school for us to pick a language, mostly C or Java, first the basic data type, then the program control statements, conditional judgment, cycle, etc., the book will teach us how to define a function, will say that the program is a piece of instructions, Tell the computer how to operate. We also see how to define a recursive function to compute the factorial or Fibonacci sequence. After work, the rest of these basics are still used day after day, but recursion is rarely used again, so that we can not use recursion to solve the problem, for this, we still have an excuse: poor recursive performance, the use of high cycle efficiency. Is that really the case? We weave a beautiful lie for the loss of one of our abilities, until more and more programming languages become popular, giving us the opportunity to see the various languages, the various styles of written procedures, only to find that we should re-examine the concept of recursion.

Why recursion is ignored

In order to answer this question, you must first speak about the programming paradigm. In all programming paradigms, object-oriented programming (object-oriented programming) is undoubtedly the biggest winner. Look at the online recruitment notices, without exception, will require candidates proficient in object-oriented programming. But in fact object-oriented programming is not a strict programming paradigm, in the strict sense of the programming paradigm is divided into: imperative programming (imperative programming), functional programming (functional programming) and logical programming (Logic Programming). Object-oriented programming is just one of the cross products of the above paradigm, and more still inherits the gene of imperative programming. Unfortunately, in the long-term teaching process, only imperative programming has been emphasized, that is, programmers to tell the computer what to do, not to tell the computer what to do. Recursion, by means of a smart function definition, tells the computer what to do. So in a program that uses imperative programming thinking, it has to be said that this is the programming way most programs now use, the chances of recursive mirrors are few, and in functional programming, you can see the recursive way everywhere. Below, we will show you how recursion can be used as a universal way to solve programming problems through examples.

A simple set of examples

How do I sum a series of integers? According to the usual imperative programming thinking, we will use loops, sequentially iterate through the list of each element to accumulate, and finally give the sum result. Such a program is not difficult to write, a little bit of programming experience can be written in a minute. This time we change the mind, how to sum in a recursive way? To this end, we might as well simplify the question assuming that the sequence contains N numbers, and if we already know the sum of the number of subsequent n–1, then the sum of the entire sequence is the first number plus the number of subsequent n–1, and so on, we can continue to add the number of n–1 in the same way until the sequence is empty, and obviously, The sum of the empty series is zero. It sounds complicated, in fact we can sum it up in one sentence: the sum of a series is the sum of the first number in the sequence plus the sequence of the following numbers. Now, let's use the Scala language to express this idea.

Listing 1. Sum of series

  Xs.head returns

the header element in the list, that is, the first element
  //xs.tail returns a listing of the remaining elements except the header element
 def sum (Xs:list[int]): Int = 
if (xs.isempty) 0 Else Xs.head + sum (xs.tail)

As you can see, we use just one line of programs to express the summation above, and this line of procedures looks straightforward. Try to write less code, which is one of the design philosophies of Scala, with less code that means easier to write, more understandable to read, and less likely to cause code errors. The same program, the amount of code written in the Scala language is usually less than half or more than Java.

The above example of summation is not special, it represents a general way of dealing with a list, that is, the operation of a list can be transformed into the same operation for the first element and the remaining list. For example, we can find the maximum value in a sequence in the same way. We assume that we already know the maximum value of the remaining series except the first element, then the maximum of the whole sequence is the first element and the largest of the remaining series. Notice here that it doesn't make sense to ask for the maximum value for an empty sequence, so we need to throw an exception out. When a sequence contains only one element, the maximum value is the element itself, which is our recursive boundary condition. A recursive algorithm must have such a boundary condition, or it will continue to recursively, forming a dead loop.

Listing 2. Find the maximum value

def Max (Xs:list[int]): Int = { 
   if (xs.isempty) 
     throw new Java.util.NoSuchElementException 
   if (xs.size = 1) C4/>xs.head 
   Else
     if (Xs.head > Max (xs.tail)) Xs.head else Max (xs.tail) 
}

In the same way, we can also find the smallest value in a series, as an exercise, the reader can go down to achieve.

Let's look at one more example: How do I reverse a string? For example, given a string "ABCD", it becomes "DCBA" after being reversed. Similarly, we can make a bold assumption that the subsequent strings have been reversed, then the first character, the entire string is reversed. For a string that has only one character, it does not need to be reversed, which is the boundary condition of our recursive algorithm. The program is implemented as follows:

Listing 3. Reverse string

DEF reverse (xs:string): String = 
if (xs.length = = 1) xs else reverse (xs.tail) + Xs.head

The final example is the classic quick sort, and the reader may find this example not simple, but we'll see that using recursion and the simplicity of Scala's language features, we just need a few lines of programs to implement a quick sort algorithm. The core idea of a fast sorting algorithm is to select a value in a unordered list that divides the listing into two parts based on that value, which is in front of the smaller portion of the value than the larger part of the value. For each of these two parts to be sorted in the same way until they are empty, obviously we think an empty list is a sorted list, which is the boundary condition in this algorithm. For convenience, we select the first element as the value that divides the list into two parts. The program is implemented as follows:

Listing 4. Quick Sort

def quickSort (Xs:list[int]): list[int] = { 
   if (xs.isempty) XS 
   else
     quickSort (Xs.filter (x=>x< Xs.head)::: Xs.head::quicksort (Xs.filter

(x=>x>xs.head)) 
}

Of course, in order to make the program more concise, the author uses some of the methods in the list here: Add an element to the list, connect two lists, and filter a list, and use a lambda expression in it. But all of this makes the program more consistent with the core idea of the algorithm and is much easier to read.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.