Functional programming and non-functional programming

后端 未结 8 1362
情书的邮戳
情书的邮戳 2020-11-28 18:02

In my second year of University we were \"taught\" Haskell, I know almost nothing about it and even less about functional programming.

What is functional programming

相关标签:
8条回答
  • 2020-11-28 18:24

    If you are looking for a good text on F#

    Expert F# is co-written by Don Syme. Creator of F#. He worked on generics in .NET specifically so he could create F#.

    F# is modeled after OCaml so any OCaml text would help you learn F# as well.

    0 讨论(0)
  • 2020-11-28 18:25

    I find What Is Functional Programming? to be useful

    Functional programming is about writing pure functions, about removing hidden inputs and outputs as far as we can, so that as much of our code as possible just describes a relationship between inputs and outputs.

    Prefer explicit when param

    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;
    }
    

    A functional language is actively hostile to side-effects. Side-effects are complexity and complexity is bugs and bugs are the devil. A functional language will help you be hostile to side-effects too.

    0 讨论(0)
  • 2020-11-28 18:28

    John the Statistician's example code does not show functional programming, because when you're doing functional programming, the key is that the code does NO ASSIGNMENTS ( record = thingConstructor(t) is an assignment), and it has NO SIDE EFFECTS (localMap.put(record) is a statement with a side effect). As a result of these two constraints, everything that a function does is fully captured by its arguments and its return value. Rewriting the Statistician's code the way it would have to look, 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 no side-effects rule, every statement is part of the return value (hence return comes first), and every statement is an expression. In languages that enforce functional programming, the return keyword is implied, and the if statement behaves like C++'s ?: operator.

    Also, everything is immutable, so localMap.put has to create a new copy of localMap and return it, instead of modifying the original localMap, the way a normal C++ or Java program would. Depending on the structure of localMap, the copy could re-use pointers into the original, reducing the amount of data that has to be copied.

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

    However, functional programs tend to run slowly (because of all the copying they have to do), and they don't tend to interact well with other programs, operating system processes, or operating systems, which deal in memory addresses, little-endian blocks of bytes, and other machine-specific, non-functional bits. The degree of noninteroperability tends to be inversely correlated with the degree of functional purity, and the strictness of the type system.

    The more popular functional languages have really, really strict type systems. In OCAML, you can't even mix integer and floating-point math, or use the same operators (+ is for adding integers, +. is for adding floats). This can be either an advantage or a disadvantage, depending on how highly you value the ability of a type checker to catch certain kinds of bugs.

    Functional languages also tend to have really big runtime environments. Haskell is an exception (GHC executables are almost as small as C programs, both at compile-time and runtime), but SML, Common Lisp, and Scheme programs always require tons of memory.

    0 讨论(0)
  • 2020-11-28 18:31

    Yes you are correct in thinking that C is a non-functional language. C is a procedural language.

    0 讨论(0)
  • 2020-11-28 18:32

    One key feature in 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 primary reason for doing so is so that successive calls to a function will yield the same result. You can write functional code in any language that supports first-class functions, but there are some languages, like Haskell, which do not allow you to change state. In fact, you're not supposed to make any side effects (like printing out text) at all - which sounds like it could be completely useless.

    Haskell instead employs a different approach to IO: monads. These are objects that contain the desired IO operation to be executed by your interpreter's toplevel. At any other level they are simply objects in the system.

    What advantages does functional programming provide? Functional programming allows coding with fewer potentials for bugs because each component is completely isolated. Also, using recursion and first-class functions allows for simple proofs of correctness which typically mirror the structure of the code.

    0 讨论(0)
  • 2020-11-28 18:39

    May be worth checking out this article on F# "101" on CoDe Mag recently posted.

    Also, Dustin Campbell has a great blog where he has posted many articles on his adventures on getting up to speed with F#..

    I hope you find these useful :)

    EDIT:

    Also, just to add, my understanding of functional programming is that everything is a function, or parameters to a function, rather than instances/stateful objects.. But I could be wrong F# is something I am dying to get in to but just dont have the time! :)

    0 讨论(0)
提交回复
热议问题