Functional Programming and Applications Flashcards
(37 cards)
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.
Give an example of a curried function?
add :: Int -> (Int, Int)
add x y = x+y
How can functions with more than two arguments be curried? Give an example.
Functions with more than two arguments can be curried by returning nested functions.
example:
mult :: Int -> (Int -> (Int -> Int))
mult x y z = xyz
Why are curried functions useful?
Curried functions are more flexible than functions on tuples,
because useful functions can often be made by applying a curried function.
example:
add’ 1 :: Int -> Int
take 5 :: [Int] -> [Int]
drop 4 :: [Int] -> [Int]
What are the two conventions of curried functions?
Convention 1: arrows associate to the right: Int -> Int -> Int -> Int means Int -> (Int -> (Int ->. Int))
Convention 2: function application associates to the left: mult x y z means ((mult x)y)z
if these conventions were not there, we would have got Int -> (Int -> (Int ->. Int)) and ((mult x)y)z
What is polymorphism?
A function is polymorphic (of many forms) if its type contains one or more type variables.
example:
length :: [a] -> Int
❖ head [] – exception raised
❖ zip [1] [1,2] = [(1,1)]
❖ take 10 [1,2,3] = [1,2,3]
What is ad hoc polymorphism / overloading?
Ad hoc polymorphism, also called overloading, is when a function’s type contains one or more class constraints.
Give an example of ad hoc polymorphism / overloading.
sum :: Num a => [a] -> a
For any numeric type a (ie, a type that is an “instance” of the class Num), sum takes a list of values of type a and returns a value of type a.
Num a is a “class constraint”, whose general form is C a, where C is the name of a class and a is a type variable.
Constrained type variables can be instantiated to any types that satisfy the constraints:
> sum [1,2,3]
6
a = Int
> sum [1.1,2.2,3.3]
6.6
a = Float
> sum [’a’,’b’,’c’]
ERROR
Char is not a numeric type
What is a higher order function?
A higher order function is a function that can be arguments of other functions, and/or can be returned as results of other functions.
Give an example of a higher order function.
twice :: (a → a) → a → a
twice f x = f (f x)