2

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.

6
  • most of your questions are to broad but for the first one: for your code so far there is absolutely no difference ... you will see differences as soon as you'll try to change the memory locations content ;) ... no really the difference is that you won't be able to change the value.
    – Random Dev
    Sep 12, 2015 at 15:51
  • 1
    Thanks. But I can simply bind a new value to x. What advantages does immutability offer?
    – Old Geezer
    Sep 12, 2015 at 16:07
  • There is a big difference between rebinding a new value to a name and change the acutal value in memory - it's not obvious for integers but look at lists: yes you can do something let xs = 5:xs (if your lang. let's you - many don't let you do this everywhere) but then the old xs will still be there and if you did use xs in another part of your programm it will still have it's value ... you just cannot shot yourself in the foot that easy - now compare this to the references you pass around in lang. like Java/C# ... see the difference?
    – Random Dev
    Sep 12, 2015 at 16:18
  • check out the "set once" concept, or "single assignment". plus all the videos by Rich Hickey where he explains it for Clojure where everything is "persistent" and so it's much easier to write concurrent code.
    – Will Ness
    Sep 12, 2015 at 16:31
  • immutability makes time explicit in the code. that's the main paradigm shift.
    – Will Ness
    Sep 12, 2015 at 17:15

1 Answer 1

2

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:

2x^2 + 7x + 6

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:

2x^2 + 7x + 6 = 0

The only choices for x are those which satisfy the equation (i.e. 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:

f(x) = 2x^2 + 7x + 6

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 (i.e. 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 (i.e. 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.

3
  • just don't call them variables ... call them values instead
    – Random Dev
    Sep 12, 2015 at 16:20
  • @Carsten No, I prefer to call them variables because they do vary with instance. Also, that's what we were taught in school: a variable is an unknown value. Unknown values were called variables long before we started calling "a value that varies with time" a variable. In mathematics, a variable still means an unknown constant value that varies with instance. Sep 12, 2015 at 17:06
  • well yes ... the point is if you write "var x = 9" then it surely is not unknown - and variable has a fixed meaning in programming that's different from what you used to in school algebra
    – Random Dev
    Sep 12, 2015 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.