Functional programming and non-functional programming

In the second year of university, we were "trained" by Haskell, I know almost nothing about it and even less about functional programming.

What is functional programming, why and / xor, where would I like to use it instead of non-functional programming, and do I correctly think that C is a non-functional programming language?

+61
glossary functional-programming paradigms
Aug 23 '08 at 14:58
source share
9 answers

One of the key features of a functional language is the concept of first-class functions. The idea is that you can pass functions as parameters to other functions and return them as values.

Functional programming involves writing code that does not change state. The main reason for this is that successive calls to the function produce the same result. You can write functional code in any language that supports first-class functions, but there are some languages, such as Haskell, that do not allow state changes. In fact, you should not do any side effects (like printing text) at all - which sounds like it could be completely useless.

Instead, Haskell takes a different approach to IO: monads. These are objects that contain the required I / O operation that your interpreter must perform. At any other level, these are simply objects in the system.

What are the benefits of functional programming? Functional programming allows coding with less potential for errors, since each component is completely isolated. In addition, the use of recursion and first-class functions allows the use of simple proofs of correctness, which usually reflect the structure of the code.

+81
Aug 23 '08 at 15:19
source share

What is functional programming

Today, there are two different definitions of "functional programming":

The previous definition (coming from Lisp) is that functional programming involves programming using first-class functions, that is, where functions are processed like any other value, so you can pass functions as arguments to other functions, and a function can return functions among them return values. This culminates in using higher-order functions such as map and reduce (you may have heard of mapReduce as the only operation that Google has heavily used, and, unsurprisingly, it's a close relative!). The .NET types System.Func and System.Action provide the higher-order functions available in C #. Although currying is impractical in C #, functions that take other functions as arguments are common, for example. Parallel.For .

A younger definition (popularized by Haskell) is that functional programming is also associated with minimizing and controlling side effects, including mutation, that is, writing programs that solve problems by writing expressions. This is most often called "purely functional programming." This has been made possible thanks to completely different approaches to data structures called “purely functional data structures”. One problem is that translating traditional imperative algorithms to using purely functional data structures usually makes performance 10 times worse. Haskell is the only purely functional programming language that has survived, but concepts have gone into basic programming with libraries such as Linq on .NET.

where would I like to use it instead of non-functional programming

Everywhere. Lambdas in C # have now demonstrated key benefits. C ++ 11 has a lambda. There is no excuse for not using higher order functions. If you can use a language like F #, type inference, automatic generalization, currying and partial application (as well as many other language functions!) Will also be useful to you.

Did I understand correctly that C is a non-functional programming language?

Yes. C is a procedural language. However, you can get some of the benefits of functional programming by using function pointers and void * in C.

+21
Jan 27 '13 at 16:15
source share

It might be worth checking out this article on F # "101" on the recently published CoDe Mag.

Dustin Campbell also has a great blog where he has published many articles about his adventures about speeding up with F # ..

Hope you find this helpful :)

EDIT:

Also, just to add, my understanding of functional programming is that everything is a function or parameters for a function, not stateful instances / objects. But I could be wrong. F # is what I'm dying to get into but just not enough time! :)

+6
Aug 23 '08 at 15:08
source share

The John the Statistician example code does not show functional programming, because when you do functional programming, the key is that the code is NOT record = thingConstructor(t) is the destination) and it has NO SIDE EFFECTS ( localMap.put(record) - statement with a side effect). As a result of these two restrictions, everything that the function performs is completely captured by its arguments and return value. Rewriting the statistical code as it would have looked if you wanted to emulate a functional language using C ++:

 RT getOrCreate (const T thing, 
                   const Function <RT <T>> thingConstructor, 
                   const Map <T, RT <T>> localMap) {
     return localMap.contains (t)?
         localMap.get (t):
         localMap.put (t, thingConstructor (t));
 }

As a result of the rule of no side effects, each operator is part of the return value (therefore, return comes first ), and each expression is an expression. In languages ​​that provide functional programming, the return keyword is implied, and the if statement behaves like a C ++ ?: Operator.

In addition, everything is unchanged, so localMap.put needs to create a new copy of localMap and return it instead of changing the original localMap , like normal C ++ or Java. Depending on the structure of localMap, a copy may reuse pointers in the original, reducing the amount of data that needs to be copied.

Some of the benefits of functional programming include the fact that functional programs are shorter and it is easier to change a functional program (because there are no hidden global effects to take into account), and it is easier to get a program in the first place.

However, functional programs tend to run slowly (due to all the copying they have to do), and they are not inclined to interact well with other programs, operating systems or operating systems that deal with memory addresses, low-north byte blocks and others machine-specific, broken bits. The degree of non-interaction, as a rule, is inversely correlated with the degree of functional purity and the rigor of the type system.

More popular functional languages ​​have really strict type systems. In OCAML you can’t even mix math with numbers and floating point or use the same operators (+ to add integers, +. To add float). This can be either an advantage or a disadvantage, depending on how highly you rate the possibility of type checking to detect certain types of errors.

Functional languages ​​also have very large runtimes. Haskell is an exception (GHC executables are almost as small as C programs, both at compile time and at run time), but SML, Common Lisp, and Scheme programs always require tons of memory.

+4
Feb 18 '09 at 0:27
source share

Yes, you are right that C is a non-functional language. C is a procedural language.

+3
Aug 23 '08 at 15:10
source share

I prefer to use functional programming to keep myself repeating work by making a more abstract version and then using it instead. Let me give you an example. In Java, I often find that I create maps to write structures and thus write getOrCreate structures.

 SomeKindOfRecord<T> getOrCreate(T thing) { if(localMap.contains(t)) { return localMap.get(t); } SomeKindOfRecord<T> record = new SomeKindOfRecord<T>(t); localMap = localMap.put(t,record); return record; } 

This happens very often. Now, in a functional language, I could write

 RT<T> getOrCreate(T thing, Function<RT<T>> thingConstructor, Map<T,RT<T>> localMap) { if(localMap.contains(t)) { return localMap.get(t); } RT<T> record = thingConstructor(t); localMap = localMap.put(t,record); return record; } 

and I will never have to write a new one again, I could inherit it. But I could do it better than inheritance, I could say in the constructor of this thing

 getOrCreate = myLib.getOrCreate(*, SomeKindOfRecord<T>.constructor(<T>), localMap); 

(where * is the "leave this option open" designation, which is a kind of currying)

and then the local getOrCreate is exactly the same as if I wrote it all on one line without any inheritance dependencies.

+3
Aug 23 '08 at 15:31
source share

If you are looking for good text in F #

Expert F # co-authored with don Sime. Creator of F #. He worked on generics in .NET specifically to create F #.

F # is modeled after OCaml, so any OCaml text will help you learn F #.

+2
Aug 23 '08 at 17:54
source share

I find What is functional programming? to be useful

Functional programming consists in writing pure functions, about removing hidden inputs and outputs as much as possible, so that most of our code as simply as possible describes the relationship between inputs and outputs.

Prefer explicit when parameter

 public Program getProgramAt(TVGuide guide, int channel, Date when) { Schedule schedule = guide.getSchedule(channel); Program program = schedule.programAt(when); return program; } 

over

 public Program getCurrentProgram(TVGuide guide, int channel) { Schedule schedule = guide.getSchedule(channel); Program current = schedule.programAt(new Date()); return current; } 

Functional language is actively hostile to side effects. Side effects are complexity, and complexity is error, and error is the devil. Functional language helps you to be hostile to side effects.

+1
Jan 07 '16 at 8:24
source share

In computer science, functional programming is a programming paradigm - a style of constructing the structure and elements of computer programs that considers computing as an estimate of mathematical functions and avoids freezing and mutable data . This is a declarative programming paradigm, which means that programming is done using expressions. In the functional code, the output value of the function depends only on the arguments that are introduced into the function, so calling the function f twice with the same value for argument x will produce the same result f (x) every time . Elimination of side effects, i.e. State changes that are independent of input functions can greatly facilitate understanding and predicting program behavior, which is one of the key motives for developing functional programming. see the wiki for more details. some examples of functional programming langues are similar to scala, javascript ... etc.

+1
Mar 16 '16 at 11:02
source share



All Articles