Functional And Dysfunctional Programming
Written by Mike James   
Thursday, 15 November 2018
Article Index
Functional And Dysfunctional Programming
State and Stateless
Monads

What is functional programming? Surely all our programs should function in some way or other? No - that's not what it means. Functional programming is altogether different....

 

What Programmers Know

knowcover

Contents

  1. The Computer - What's The Big Idea?
  2. The Memory Principle - Computer Memory and Pigeonholes
  3. Principles of Execution - The CPU
  4. The Essence Of Programming
  5. Variables - Scope, Lifetime And More
  6. Binary Arithmetic
  7. Hexadecimal
  8. Binary - Negative Numbers
  9. Floating Point Numbers
  10. Inside the Computer - Addressing
  11. The Mod Function
  12. Recursion
  13. The Lost Art Of The Storage Mapping Function *
  14. Hashing - The Greatest Idea In Programming
  15. XOR - The Magic Swap
  16. Programmer's Introduction to XML
  17. From Data To Objects*
  18. What Exactly Is A First Class Function - And Why You Should Care*
  19. Stacks And Trees
  20. The LIFO Stack - A Gentle Guide*
  21. Data Structures - Trees
  22. Inside Random Numbers
  23. The Monte Carlo Method
  24. Cache Memory And The Caching Principle
  25. Data Compression The Dictionary Way
  26. Dates Are Difficult
  27. Magic of Merging
  28. Power of Operators
  29. The Heart Of A Compiler
  30. The Fundamentals of Pointers
  31. Public Key Encryption
  32. Quick Median
  33. Functional And Dysfunctional Programming*

* Recently revised

Functional

 

More cartoon fun at xkcd a webcomic of romance,sarcasm, math, and language

 

Math - don't you just love it.

So much so that a lot of programmers wish that programming was more like math. There have been lots of attempts to make programming precise in the sense that programs can be verified just like a mathematical proof. The power of math is such that anything that makes messy programming look more like perfect math.

But there are so many ways in which programming isn't like math at all. The big difference is that programming as we practice it mostly includes the ideas of time and state changes.

A program isn't a static statement of some universal truth; rather it is a set of instructions that have a time sequence built in. A program doesn't even follow the same route each time it is executed. It reacts to the state of the environment around it and it can change its own state each time it is run. 

A program is so much more than a mathematical proof. 

If you want to confuse a mathematician then write on one side of the card:

"The statement on the other side of this card is true"

and on the other side:

"The statement on the other side of this card is false"

and hand it over.

The result is usually a mathematician in melt down.

A programmer on the other hand  is happy to think that the state changes as the card is flipped. The statement on the side you are looking at is true. Flip the card over and the statement on the other side becomes false and so on. You have a flip flop that changes state as you turn the card over. 

Don't take this example too seriously because if you do you will miss the point and risk vanishing in a puff of logic.

The difference between programming and math is that in programming you can, and probably must, write things like:

x=x+1

but in math this is just silly. So silly that you can use it to prove that 1=0 by subtracting x from both sides.

This difference is one of the reasons beginners find programming difficult. Until they learn to see time as an element of what they read and write, it all seems very alien and the idea of a variable is mysterious. 

To be fair, math does have variables, but they are used in very restricted ways compared to the way they are used in programming; and where they are used more like as in programming then the math begins to look like programming. Math mostly deals with change by taking a snapshot and making everything static - what else is a function in the f(t) sense other than frozen time? It isn't a dynamic process but a subset of the Cartesian product of two sets. If this is too abstract think of a function as a graph or chart of f against t. Nothing dynamic here.



Last Updated ( Monday, 26 November 2018 )