Software 3

Credits: 10

Module Description

Topics:

  • Block 1 - Intro to functions
    • Why functional programming?
    • Defining functions
    • Thinking recursively
    • Haskell type system
    • Notes on Block 1
  • Block 2 - Lists and more functions
    • Lambda calculus
    • List comprehension
    • Folding lists
    • Lazy evaluation
    • Notes on Block 2
  • Block 3 - Reasoning and type classes
    • Reasoning about programs
    • Type classes
  • Block 4 - IO & Monads
    • Effects through monads
    • Fresh identifier generation using monads

Block 1 - Intro to functions

Why FP?

There are a number of reasons why functional programming languages excel over regular:
  1. Value Orientation: the language is value-oriented meaning all occurences of a name refer to the same value. This is also called being referentially transparent. It means that it's easier to argue about the results of programs, as you can use equational reasoning. It also means that it's easy to encode many useful algebraic structures in the libraries, due to the simple mathematical structure. These languages are also called pure due to the lac of ad hoc side effects.
  2. All values are first class: this means that functions themselves are treated like any other value, so functions can have functions arguments and function results. It means we can easily abstract away computations and computational structures, and instantiate them as necessary, leading to shorter and better structured code.
  3. Laziness for better modularity: the language is lazy, which means all functions are 'call-by-need'. It is an evaluation strategy that only evaluates an expression when it is needed, and only the part that is needed, allowing us to seperate control and data in new ways. Only pure languages can be lazy.
  4. Powerful type systems: some languages can optionally have statically decidable type systems. Type systems can protect programmers from a large number of errors, at cost of some freedom to the programmer. Functional lanuages have very powerful but flexible type systems. Haskell uses a statically decidable type system, giving lots of protection with little loss of freedom.
  5. Difference: the feautures of Haskell makes it a very different language to traditionally learned OOP languages. It allows us to have a deeper understanding of programming and pprogramming languages, and give us new ways of seeing and solving problems.

Notes on block 1

Cons (:) vs concatenate (++)

Seemingly, : and ++ seem to function the same. However, cons prepends a given item to a list e.g. 5:[] = [5]. Conccatenate, on the other hand, adds a list (on the right) to the end of the list (on the left) e.g. [1,2] ++ [3] = [1,2,3]. The differences are that cons takes a list element and a list, whereas concatenate takes two lists and joins them together.

Block 2 - Lists and more functions

Notes on block 2

Using the function 'filter'

If we want to select some elements from a list depending on some condition, we can use 'filter'. Syntax is filter (condition) (list).

Monads

A monad is just a monoid in the category of endofunctors, what's the problem?

Seriously though, monads are simply a way of performing side effects without destroying the purity of the language. They cleanly seperate side-effecting computations with those that don't have side effects, so neither computation interferes with the other.
Monads are a generalization of functions, function application, and function composition to allow them to deal with richer notions of computation than standard functions.
An example of a monad is the monadic type and function 'Maybe' and 'maybe'. Monadic values are not intuitive, they are just ways of represnting the output of a monadic function. The maybe function can error check at the same time as performing a function (see question 2.2)

Notes on Block 3

type vs newtype vs data

newtype and data are very similar and the differences are only in run-time compiling, when data is treated as a completely new type, whereas a newtype is indistuinshable (i.e. same structure) from the type it uses.

'type' is just a way to create type synonyms, and is exactly the same of the type it uses at runtime.