Python: List Presentation and Functional Programming

In my Python study book, when I read List Comprehension , the author has a small note in the book:

Understanding Pythons Lists is an exam to support functional programming concepts ....

I go to the Wiki to read about functional programming. But it’s hard for me to imagine, because I don’t see anything between the List Comprehension and this concept on the wiki page.

Please give me a clear explanation (and if so, give me some more examples about functional programming in Java or C #: D)

Thanks:)

+6
source share
5 answers

If your question is “give me some examples showing how FP works in python” then:

What is pure functional programming (in Python)?

This is a programming paradigm that avoids state and mutable data and instead relies on the return values ​​of a function. This means that a purely functional program written in python will not have such things as variables, states, etc.

Not very clean FP

You can combine FP and the imperative paradigm with good results (see here ). The linked gist is a math quiz program that I did for the python class that I took a few months ago. Feel free to do whatever you want with the code.

FP in Java / C #

I personally have no experience with C #, so someone else would need to post a C # example, but you can have FP in Java, but not purely FP. Example:

 int fib (int x) { if (x < 2) return x; return fib (x-1) + fib(x-2); } 

The method above is completely FP, but it cannot be used in a pure FP context when using Java. This should be placed inside the C class in Java and can only be called after you have created an instance of this type of object. This last part will disqualify the Java C class from FP, but the method will still be.

Edit: in fact, you can have static methods in Java that you can use without any instantiation. Therefore, if you change the signature to static int fib (int x) , then the method and method calls can still be FP if they are called in an FP manner.


Re: your comment

The recursion can be FP, but it does not have to be (see below):

 def f(first, rest): print first first = rest[0]; rest = rest[1:] f(first, rest) 

You can also have FP without recursion:

  def sum (a,b): return a+b def square(c): return c*c def square_of_sum (x,y): return square(sum(x,y)) 
+6
source

Python map (), reduce () and filter () execute the sequence, apply another function of your choice to it, and then return a different sequence for you, leaving the original sequence intact.

We can say that this functional, since it does not relate to the initial sequence, does not relate to its internal state and does not cause side effects. (although a function that you yourself provide can do some of the above, for example, create a side effect).

Functional programming - it's a different way of programming and structure of your application in order to reduce errors caused by side effects (changes in the values in another static location or process directly), and to reduce or eliminate the need to synchronize access to shared data. Some languages ​​force you into this, for example erlang , while others leave you more to choose which path suits you best at that time (procedural or functional), with a preference for the functional side of the programming spectrum, for example scala

+4
source

I believe that the Pythons List is understood directly from Haskell (a very "clean" functional language).

Haskell:

 [ x | x <- [1..10] ] 

Python:

 [ x for x in range(1,11) ] 

as people have noted, Python allows you to use functional concepts like map() , reduce() and lambda

although these are all functional ideas, they can rarely be used in a purely functional form, since Python is not recursive.

if you want to learn about "functional" languages, look at "Haskell", "Scala", "Clojure", "Erlang", "F #" ... which are all more or less functional (although some may suggest that this not this way)

And if you really want to understand what functional programming is, look here. Find out that you have haskell for a good one that is easy to read, has nice shots and will open your eyes.

EDIT -

Examples of Haskell's factorial functions (they all do the same thing):

 fact1 0 = 1 fact1 n = n * fact1 (n - 1) fact2 n | n == 0 = 1 | otherwise = n * fact2 (n - 1) fact3 n = case n of 0 -> 1 _ -> n * fact3 (n - 1) 

ps, look at the question , and this is also relevant.

+4
source

I am sure that others will be able to explain this better than I will, but functional programming is simply basically related to how you think about the program flow and whether you can pass functions as objects to calculate on. For example, in javascript, when you provide a function that will execute when even operations are passed around the function, and in this sense it is almost like functional programming.

This is a meaning in which understanding the list is like functional programming, because you are giving instructions on how to calculate each element, not a more procedural approach that would have to execute the loop and do the calculations themselves, and not pass it as a function. Python is not what I consider to be a true functional programming language like LISP or ML or Haskell (erlang? I don’t remember), but it can do something like this (look at lambda expressions in python).

Java and C / C ++ are not very functional, but you can mimic it with function pointers as arguments. Not familiar with C # ...

Event-based languages ​​generally use this idea of ​​a function, passing it simply because they need to somehow convey unknown code that will be executed later.

+1
source

I just think the terms Map and Abbreviation refer to Lisp and functional programming.

and python has

filter, display and reduce

ref: http://www.joelonsoftware.com/items/2006/08/01.html

  http://docs.python.org/tutorial/datastructures.html 
0
source

Source: https://habr.com/ru/post/917677/


All Articles