Concepts In Functional Programming
Podcast: Play in new window | Download (46.0MB) | Embed
Subscribe: Apple Podcasts | Spotify | Email | RSS | More
To begin some basic concepts of almost any programming language include variables, control structures, data structures, syntax, and tools. Variables are ways of storing information to be used in your code. Typically they are labeled with a ‘symbolic name’ which describes the information they store though they are sometimes obscured. The value or information can be retrieved by referring to the name. Control structures are the decision making part of your code. They analyse variables and makes a decision based on a strict set of rules to determine the flow of the code through your program. Data structures are the way the computer stores and organizes data. Syntax is the rules or writing code like grammar is the rules of writing human language. This varies the most between languages. Finally, tools are parts of a language used to make coding easier.
“Functional code is characterized by one thing: the absence of side effects.” ~ Mary Rose Cook
Functional programming is not new. Haskell is one of the most purely functional languages and has been around since 1990. It is a programming paradigm or way of structuring code. Some languages can fall into multiple paradigms depending on how the code is written in them. Some paradigms are concerned with the execution of the code whereas others are concerned with the organization of the code and others with the syntactic style.
Episode Breakdown
12:55 The Functional Paradigm
The functional paradigm uses expressions or declarations rather than statements. Expressions are combinations of values and functions that are computed to produce another value. In functional programming the result of an expression can be another function to be evaluated.
Computation is treated as the evaluation of mathematical functions. This avoids changing state or mutable data. Outputs are always the same with the same inputs and are not state dependant.
“If you think of this as something bolted onto algebra it makes a lot more sense.”
One of the biggest advantages of functional programming is the ability to run programs concurrently. When a function can change an object’s state it can be a mess for concurrency if the state is not managed properly.
16:29 Based on Lambda Calculus
“It’s meta math.”
All functions can be written anonymously. The only thing effecting the function is the arguments passed into it. This is where Lambda (anonymous) functions get their name.
All functions go through currying. When invoked with multiple arguments a function will only set one variable in the list. Then it returns a function with one less argument that is immediately invoked. This only works because state changes cannot effect the pure function.
“It’s something that makes sense given the other restrictions.”
Lambda calculus and therefore functional programming contain free and bound variables. A bound variable is bound to the lambda expression and can be replaced by any input. A free variable is a variable within the expression that is not effected by the input. Beta reductions is the concept that bound variables can be replaced by the inputs they represent.
22:07 Functions Are Pure
A function called with the same input will always have the same output. The function works the same independent of state. If you call add(2,2) it will always return 4 regardless of big brother.
“If you change the state of the program it’s going to give you the same output given the same inputs.”
No side effects occur. A side effect is the modification of something outside the scope of a a function such as modifying a global variable, raising an exception, writing to a file, or even reading data. This is the most common way programs interact with the outside world.
25:15 Variables Are Immutable
“In other words variables…don’t!”
Variables cannot be modified after initialization. Once it’s set, it’s set. This maintains state throughout runtime. However, you can create new variables. The constructor is the only way to mutate state by creating a new variable, the new variable will not change either once initialized.
27:00 Functions are First-Class and Higher-Order
This is not exclusive to functional but required for it. First-class functions can be set to a variable. They treat the function like a normal data type and execute it at another point in runtime.
“A variable is a function, basically.”
Higher order functions accept or return a function. For example, map functions that iterate over a list, modify data, and return new list. Or filter functions that return a new list of selected items
31:20 Referential Transparency
You can replace a function with the value it returns everywhere in a program and the state of the program will not change. Take all the instances where you call a function and replace them with the output of that function. To be referentially transparent there should be no changes when this is done.
33:07 How do you write to a database or file system?
“Most of what I code is a functional/object oriented blend.”
Be pragmatic in don’t write pure functional code. Write the code that works. Functional, object oriented, and procedural are all paradigms or ways of thinking about or structuring your code. To be effective you’ll need to combine paradigms at times.
IoTease: Project
Game System in an Xbox Controller
Using a Raspberry Pi Zero and an old Xbox controller this tutorial shows you how to build your own game console in the controller. You’ll start by taking apart the controller and placing the Pi inside the controller. Convert the Xbox controller cord to USB then basically plug the controller into the Raspberry Pi in the controller. Flash the RetroPi games onto the Raspberry Pi Zero and with a little configuration you have an entire game system in a controller.
Hardware
- Raspberry Pi Zero
- Original Xbox Controller
- USB OTG Cable
- 2A USB Power Supply
- Mini HDMI Adapter
- MicroSD Card
Tricks of the Trade
Academics explain things for other academics. Not for you. If you don’t understand it, get rid of some complexity and simplify it until you do. You can always add complexity back in later. Academics don’t understand Gall’s law, thus they like to express complex systems at the highest level of complexity possible, rather than explaining a simple system first and then building up to the complex expression of it.