A concrete example of functional knowledge that allows you to write the best imperative / OO code

I have been working with functional programming for a while, and I think it's great, so I would like to teach some of my friends Haskell.

Unfortunately, I can’t come up with any particular piece of code to show them and say "Look, this is how it would look convincing, look how much better functional"

So, can anyone who is more expert than me, and that this very low requirement, help me?

This did not seem to affect me, but in case, please tell me how to fix it.

+6
source share
4 answers

Probably the best ideas about transference are the so-called “semantics of meanings” and “purity”.

Each of them plays so hard that it is difficult to separate them in practice. In principle, however, the semantics of meanings mean that each “thing” should act as a value instead of an object. This leads to a simpler passage, less "eerie actions at a distance" from the state, and provides some background for performing equational reasoning on the code. Cleanliness means that side effects do not occur wherever you have the code, but instead only at carefully delimited points. This means that most of your code ends up independent and reusable, while only the main “application” bits confuse themselves deeply with state and effect.

You can say that purity has all the semantics of meaning or meanings are pure calculations, so it may be worth saying that “meanings” refer to the nouns (statics) of your system and the “purity” of verbs (dynamics).


These methods are well known as useful in other languages. It is a common idea in OO languages ​​these days to gladly sacrifice some speed for value semantics due to the advantages of organization and correctness. If you calm down with Haskell, then you will understand how semantics and purity of meaning work if they apply to every aspect of the whole program without compromise. This means that you come across some powerful templates for reasoning and creating clean programs.

One place that I was going to make for comparison is the free monads and the team template. Both solutions to very similar problems are "how can I make an explicit structure containing instructions that should be executed by the program and execute it later, possibly in different ways?" - but the command pattern tends to dance around great variability in at least the interpreter, if not the teams themselves.

Can we write team templates that look more like Free monads? What would be the benefits? These are the questions that you can ask with far more urgency if you have a strong Haskell background.

+7
source

This is an interesting and difficult question. For some time, there has been a tendency for the concepts of functional languages ​​to come into imperative languages, and the line between functional / imperative languages ​​is quite blurred. For example, suppose you want to compose each element of the xs list and store the result in a new ys list.

 >> xs = [1, 2, 3] # Python 

 >> xs = [1, 2, 3] -- Haskell 

If you did not know about functional idioms, you can do this:

 >> ys = [0] * len(xs) # Python >> for i in range(len(xs)): ys[i] = xs[i] * xs[i] 

In Haskell, you just write

 >> ys = map (\x -> x * x) xs -- Haskell 

This idiom also exists in Python, of course

 >> ys = map(lambda x: x * x, xs) # Python 

Perhaps an even more convenient way to write this is to use a list

 >> ys = [x * x | x <- xs] -- Haskell 

which also exists in python

 >> ys = [x * x for x in xs] # Python 

Of course, the functional way is much nicer (and more complex, more reusable) than the imperative way. But in this case, you do not need to use a functional language for profit - you just need to be prepared to "think functionally."

+6
source

Monads and sequels.

I came across this code:

 synchronized(q) { Object o = q.poll(); if (o == null) { ...// do something } } 

This compiled itself into a much nicer refactoring API:

 Object o = q.poll(doSomethingAsLambda) 

The q implementation has been rewritten, of course, but now the synchronization is more subtle, because the implementation allows you to execute custom code “inside” the branch that the q implementation will know about.

+2
source

Modern functional languages ​​(like Haskell and ML) are concise - you can talk a lot in small code.

The real benefit of consensus is that you can quickly iterate the design, and in other areas (for example, graphic or clothing design) fast iteration is apparently considered one of the “tools” to become a good or expert designer; therefore, a fair belief that learning how to quickly iterate software products will help make you a better programmer.

Of course, modern scripting languages ​​such as the Python programming languages ​​and "design" such as Alloy (developed by Daniel Jackson at MIT) are concise and allow you to quickly iterate through projects / prototypes. Thus, a more significant topic is that lightweight / concise languages ​​help you iterate and improve your design skills, rather than just “functional programming making you a better programmer”.

+2
source

All Articles