In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Haskell language case Analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Haskell language case Analysis" article.
Example:
Quicksort:: Ord a = > [a]-> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) + + [p] + (quicksort greater) where lesser = filter (
< p) xs greater = filter (>= p) xs
I'm confused. Can it be right that it is so simple and beautiful? Indeed, this writing is not a "completely correct" quick sorting implementation. However, I do not want to delve into performance issues here [2]. I would like to emphasize that pure functional programming is a change of thinking, a completely different mode of thinking and method of programming, which means that you have to start learning another way of programming.
First, let me define a problem and then solve it in a functional way. Basically all we have to do is sort an array in ascending order. To accomplish this task, I used the quick sort algorithm that changed our world [3]. Here are a few basic sorting rules:
If the array has only one element, return the array
When there is more than one element, randomly select a base point element P and divide the array into two groups. Make all the elements in the * group p. Then use this algorithm recursively for these two sets of data.
So, how to think and program in a functional way? Here, I will simulate the two inner conversations of the same programmer, which are very different. One uses the imperative programming mode of thinking, which is the way the programmer learned to code from the beginning. And the second mind has made some ideological changes to get rid of all the prejudices that have been formed before: thinking in a functional way. In fact, this programmer is me, who is writing this article right now. You will see two completely different me. There is no lie.
Let's compare with Java in this simple example:
Public class Quicksort {private int [] numbers; private int number; public void sort (int [] values) {if (values = = null | | values.length = = 0) {return;} this.numbers = values; number = values.length; quicksort (0, number-1);} private void quicksort (int low, int high) {int I = low, j = high Int pivot = numbers [low + (high-low) / 2]; while (I pivot) {j muri;} if (I [a]-> [a]
JAVA:
I'm going to start simply, if it's an empty array, if the array is empty, I should return this array. But... Damn it, the program crashes when the array is null. Let me add an if statement at the beginning of the sort method to prevent this from happening.
If (values.length = = 0 | | values = = null) {return;}
HASKELL:
To start with simply, an empty list. In this case, pattern matching is required. Let me see how to use it. Okay, great!
Quicksort [] = []
JAVA:
OK, now let me use recursion to deal with the normal situation. Normally, you need to record the state of the sort method parameters. I need its length, so I also need to add a new property to the Quicksort class.
Public void sort (int [] values) {if (values.length = = 0 | | values = = null) {return;} this.numbers = values; this.length = values.length; quicksort (0, length-1);}
HASKELL:
This is already recursive. You don't have to do anything anymore.
No code. Nothing. Nada. That's good.
JAVA:
Now, I need to implement a quick sort process according to the rules described above. I choose * * elements as the base element, which does not require the use of other strange methods. Comparison, recursion. Each comparison traverses from both ends at the same time, one from beginning to end (I, the list that generates p). Each time a larger current value is found in the I-direction traversal than the j-direction traversal, their positions are interacted. When the position of I exceeds j, the comparison is stopped and recursive calls continue to be made for the two new queues formed.
Private void quicksort (int low, int high) {int I = low, j = high; int pivot = numbers [low]; while (I pivot) {j Murray;} if (I = p) xs
I tried to explain in the most detailed language that Java uses iteration + recursion for quick sorting. But what if in the java code we don't write an iTunes loop and we get a while loop condition wrong? Well, this is a relatively simple algorithm. But we can imagine what would happen if we wrote code like this all day and faced programs like this all day, or if the sorting was just the step of a very complex algorithm. Of course, it can be used, but it will inevitably generate potential, internal bug.
Now let's take a look at these statements about status. If for some reason the array is empty and becomes null, what happens when we call this Java version of the quick sort method? There is also a performance problem with synchronous execution. What if 16 threads want to access the Quicksort method at the same time? We need to monitor them, or have each thread have an instance. It's getting messy.
It finally comes down to the problem of the compiler. The compiler should be smart enough to guess what to do and how to optimize [5]. Programmers should not think about how to index and how to deal with arrays. Programmers should think about the data itself and how to transform the data as required. You might think that functional programming adds complexity to thinking about algorithms and processing data, but that's not the case. It is the imperative programming thinking that is popular in the programming world that hinders us.
In fact, there is no need for you to give up your favorite imperative programming language and switch to Haskell programming. Haskell language has its own defects [6]. As long as you can accept functional programming thinking, you can write better Java code. You can become a better programmer by learning functional programming.
Take a look at the following Java code?
Public List sort (List elements) {if (elements.size () = = 0) return elements; Stream lesser = elements.stream () .filter (x-> x.compareTo (pivot))
< 0) .collect(Collectors.toList()); Stream greater = elements.stream() .filter(x ->X.compareTo (pivot) > = 0) .cake (Collectors.toList ()); List sorted = new ArrayList (); sorted.addAll (quicksort (lesser)); sorted.add (pivot); sorted.addAll (quicksort (greater)); return sorted;}
Is it very similar to Haskell code? Yes, maybe the version of Java you're using doesn't run it correctly. Here you use the lambda function, a cool syntax introduced in Java8 [7]. See, functional syntax not only makes a programmer better, but also makes a programming language better.
Functional programming is the result of the natural evolution of a programming language to a higher abstract stage. Just as we think that developing Web applications in C is very inefficient, over the years, we also think that imperative programming languages are the same. Using these languages is a compromise for programmers in terms of development time. Why do so many startups choose Ruby to develop their applications instead of using Clippers? Because they can make the development cycle shorter. Don't get me wrong. We can compare a programmer to a cloud computing unit. An hour for a programmer is much more expensive than an hour for a high-performance AWS cluster server. By making it harder to make mistakes, making bug less likely, and using higher abstract designs, we can make programmers more efficient, creative, and valuable.
Dimensions:
[1] Haskell from scratch courtesy of "Learn you a Haskell for Great Good!"
[2] This quicksort in Haskell that I am showing here is not in-place quicksort so it loses one of its properties, which is memory efficiency. The in-place version in Haskell would be more like:
Import qualified Data.Vector.Generic as V import qualified Data.Vector.Generic.Mutable as M qsort:: (V.Vector v a, Ord a) = > v a-> v a qsort = V.modify go where go xs | M.length xs < 2 = return () | otherwise = do p
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.