Struggling with the basics

I am trying to self-learn SML . Though I can write some SML code, I have not attained the ah ha.

In:

val x = 5;

how does binding a value of 5 to the name x differ from assigning a value of 5 to the memory location/variable x in imperative programming?

How does the above expression elucidate "a style that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data"?

What do I have to throw away about imperative programming to catch on FP quickly?

Please be gentle with me.


How does binding a value of 5 to the name x differ from assigning a value of 5 to the memory location/variable x in imperative programming?

The key difference between variables in functional programming and variables in imperative programming is that variables in functional programming cannot be modified.

Then why are they called “variables”?

Variables in functional programming languages don't vary in the sense that you can modify their value. However, they do vary in the mathematical sense in that they represent some unknown constant value. For example, consider the following quadratic expression:

This is a quadratic expression in one variable. The variable x varies in the sense that you can select any value for x . However, once you select a certain value for x you cannot change that value.

Now, if you have a quadractic equation then the choice of x is no longer arbitrary. For example, consider the following quadratic equation:

The only choices for x are those which satisfy the equation (ie x = -2 or x = -1.5 ).

A mathematical function on the other hand is a relation between two sets, called the domain (input set) and the codomain (output set). For example, consider the following function:

This function relates every x belonging to the set of real numbers to its corresponding 2x^2 + 7x + 6 , also belonging to the set of real numbers. Again, we are free to choose any value for x . However, once we choose some value for x we are not allowed to modify it.

The point is that they are called variables because they vary in the mathematical sense that they can assume different values. Hence, they vary with instance. However, they do not vary with time.

This immutability of variables is important in functional programming because it makes your program referentially transparent (ie invoking a function can be thought of as simply replacing the function call with the function body).

If variables were allowed to vary with time then you wouldn't be able to simply substitute the function call with the function body because the substituted variable could change over time. Hence, your substituted function body could become invalid over time.

How does the above expression elucidate "a style that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data"?

The expression val x = 5; could be thought of as a function application (fn x => y) 5 where y is the rest of the program. Functional programming is all about functions, and immutability in the sense that variables only vary with instance and not with time.

In particular, mutability is considered bad because it is implicit. Anything that is implicit could potentially cause unforeseeable errors in your program. Hence, the Zen of Python explicitly states that:

Explicit is better than implicit.

In fact, mutable state is the primary cause of software complexity. Hence, functional programming tries to eschew mutability as much as possible. However, that doesn't mean that we only use immutable structures. We can use mutable structures. We just need to be explicit about it.

What do I have to throw away about imperative programming to catch on FP quickly?

Nothing. Functional programming and imperative programming are two ways of thinking about computation. Computation is a very abstract idea. What exactly is computation? How do we know which problems can be computed? These were some of the questions that mathematicians struggled with in the early nineteen hundreds.

To think about computation we need to have a formal model of computation (ie a formal system that captures the notion of computation). There are various models of computation. However, the most famous are the lambda calculus (which gave rise to functional programming) and turing machines (which gave rise to imperative programming).

Both the lambda calculus and the turing machine are equivalent in power. They both capture the notion of computing and every problem that can be computed can be expressed as either a lambda calculus expression or an equivalent turing machine. The only difference is the way in which you express your problem.

You don't have to throw away anything that you learned about imperative programming to understand functional programming. Neither one is better than the other. They are both just different ways of expressing the same problem. However, you do need to start thinking like a functional programmer to write good functional programs.

链接地址: http://www.djcxy.com/p/80848.html

上一篇: 用clojure进行函数式编程,避免可变状态

下一篇: 苦于基础