Some concepts in Haskell and Python

Source: Internet
Author: User

From: http://blog.csdn.net/tangboyun/archive/2010/04/03/5447688.aspx

First of all, it must be declared that the content below is just some of my personal experiences and insights. Limited by my personal experience, it will certainly be somewhat confusing or even misleading.

Two days ago, I linked a funny picture of language fans at why learning Haskell/Python makes you a worse programmer, as shown above. At that time, I didn't think about it, but I thought it was just fun. Today, when I learned about the lazy Io feature in Haskell, I suddenly figured out some questions that no one could answer since I learned Haskell myself. At the same time, I also understood why Haskell has such a strange and unaccustomed feature.

Everything is in the picture above. It is not just for fun, but part of it is a real fact: everything you do in Haskell, this is what Einstein wrote on the blackboard on the picture. "You have been writing formulas all the time ".

What does it mean? In a procedural programming language with C as the originator, variables are variables and functions are functions. A variable represents the storage of data, and a function represents an operation. You may say, no, you can have a function pointer. Why do variables and functions in programming belong to different families? Lazy evaluation is missing in procedural languages.

When you write:

 

View plaincopy to clipboardprint?
  1. Typedef int myfunty (const char *)
  2. // Some code
  3. Myfunty fp = atoi

 

When the program runs to the third row, the FP contains the function address. Therefore, "=" is called "value assignment" in C and the value is data. The position of each line of the program written in procedural programming indicates the "execution order in actual running". Each row is completed, save the result for future use. Therefore, writing C Programs is more like writing a complete protocol experiment,

Then, we give it to the obedient students for execution. These three good students are never lazy. What the teachers say (that is, the programmer you), they do what they say, where they do it. (Just like a soup puppet)

In Haskell, there is no data concept. Everything is a function, and there is no "assign value" in a strict sense. It assigns a relationship or installs B, given a ing, "=" indicates a declaration and definition meaning (in C/C ++ ). Everything you do is defining a f (x) = ....., since there is no "data", you must have a function. You must support the local Declaration of the function (this is a local variable). Write the entire program as f (x) = g (H (I (J (X) So recursively defined, also because of this, Haskell is so fancy to the pure attribute of the function (pure function, the same input must obtain the same output). All input and output operations are impure .... Do you still remember the math test in the fourth and fifth grades of elementary school? I vaguely remember what the last few questions were called "Application Questions" at that time. The solution was to write a equations according to the question, such as Y = ax + B, then, according to the number given by the question, X is substituted into the number, and then the problem can be solved. In Haskell, lazy evaluation is used instead of iteration when your program is written. For example, when I was a child during the cold and summer vacations, I was confused at the beginning. In the last week or two, wow, I had to submit my homework during the holidays. What should I do, the last day of the holiday ....

Function-based programming and procedural programming have different implementations in OOP, but Haskell is not really oop. In C ++, programs use classes, that is, the data and related functions, as the building blocks of the entire program, are finally built together. Haskell first splits the program into two halves, half of which is the pure part, that is, the internal operation, which does not involve Io, this part is composed of various types of multi-state function splicing modules (enhanced the concept similar to the shell channel), while the other half is composed of the impure part of the IO, and finally mounted to the module by the interface, make up a program, but this part, let me talk about it now, the skill is too far and too far away .........

It is also interesting that it is the Python language, because I have rummaged through the python learning manual for a week (I accidentally revealed the essence of waste). Maybe it is because the father of Python has a mathematical background, obviously, I found the traces of Haskell in pyhon.

First, the variable naming in python is definitely not a type of strong procedural method like C/C ++, it is also different from Perl's weak type (although Perl is a weak type, it cannot be bound to symbol at Will). Python's processing is more like using symbols in the symbol table directly by the compiler, you can bind any data type or function at will. Obviously, in Python, variables are only a ing relationship f (x) and are not any entity.

Second, Python also has a local definition of the function and lazy evaluation. The function can be nested in the function, or in if .. /else .. in this type of conditional discriminant, the same symbol is bound to different functions based on the condition judgment result, which is proved by Python's lazy evaluation. That is, the function used must be determined at runtime, which cannot be implemented in the source code parsing stage of the literal translator.

Third: the third point, with the above two points, can be easily inferred. That is, the current Python literal translation must be strictly indented and consistent (Haskell also needs to be indented. Why ?), This is not an incidental product, or, as mentioned in General Recommendation books, cloud is required to keep the code beautiful. No, this is not the case. This requirement is because if a special syntax is not defined for a local function (Lambada function is not a local function mentioned here, because it is not a Conditional compilation, lambada is just a syntactic sugar), so it cannot be distinguished from global function definitions. If all functions are parsed at runtime, the performance will be terrible. Therefore, the indentation is consistent because the syntax requires this (otherwise, you must modify it and add a keyword for the local Function Definition). Otherwise, the compiler cannot complete the inference, it also supports Conditional compilation of lazy evaluation.

 

Related Article

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.