Functional Programming and Applications Flashcards
What is a programming paradigm?
A programming paradigm is a way to categorise programming languages based on their features.
What is imperative programming?
Instruction sequences for a von Neumann computer
How are functions coded in imperative programming?
Functions are implicitly coded in every step required to solve a problem.
What is functional programming?
A programming paradigm without mutable variables, assignments, loops and other imperative control measures.
How does functional programming create maintainable software?
It uses pure functions to create maintainable software
How do you compare imperative and functional programming paradigms?
Imperative:
Program: a sequence of instructions for a von Neumann m/c.
Computation by instruction execution.
Iteration.
Modifiable or updatable variables. (Mutable data structures)
Does not support independent understanding - harder to reason about.
Low level specification (HOW)
Functional:
Program: a collection of function definitions (m/c independent).
Computation by term rewriting.
Recursion.
Constants. (Immutable data structures)
Supports independent understanding.
High level specification (WHAT)
Show that, for any number x, sum [x] = x.
The only elements of [x] are x.
Sum sums the elements inside of the list such as sum []
essentially this is x + 0 = x
so sum [x] = x
sum [x]
= x + sum [] (applying sum)
= x + 0 (applying sum)
= x (applying +)
Define a function product that produces the product of a list of numbers, and show using your definition that product [2,3,4] = 24
product [] = 1
product (n:ns) = n * product ns
eg.
product [2,3,4]
= 2 *(product [3,4])
= 2 ( 3 (product [4]))
= 2 ( 3 ( 4 *(product [])))
= 2 ( 3 (4 * (1)))
= 24
How should the definition of qsort be modified so that it returns a list in its descending order?
Swap smaller and larger
qsort [] = []
qsort (x:xs) = qsort larger ++ [x] ++ qsort smaller
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
What is a type?
A type is a name for a collection of related values.
What is type inference?
The process during which every well formed function’s types can be calculated during compile-time.
What is a list?
A sequence of values with the same type. Any length.
What is a tuple?
A sequence of values of different types. Fixed length.
What is a function?
A mapping of values from one type to values from another type.
eg. Char -> Char, Char -> Bool
What is a curried function?
A curried function is a function that takes multiple arguments one at a time, and returns a function as a result.