# LGA-02: Functional Progamming¶

Note

This LGA has many parts, but each part need only be completed **twice**
(for redundancy) when considering your group as a whole. Before you leave
class, make sure to split up the work. The work should be split so that
everyone has a equivalent number of questions to do.

Answer these questions about

**referential transparency**:- Research the term and create your own definition for
**referential transparency**. Make sure your definition makes sense to someone who has only ever programmed in C++ before. - Provide an example C or C++ function that is not referentially transparent (and don’t just copy-paste the one from Wikipedia).
- What is
**memoization**? How does referential transparency allow memoization? - How does referential transparency allow you to prove functions correct?

- Research the term and create your own definition for
Answer these questions about

**eager vs. lazy evaluation**:- Research the term. Create your own definition of each, and prepare to explain the difference to your learning group.
- How does lazy evaluation allow for lists of infinite length?
- Under what circumstances might the result of a lazy evaluation differ from the results of a eager evaluation? Can you provide an example (any language, or even pseudocode, will do)?

Haskell is a statically typed language, but it is not always necessary to write the type in the code. Answer these questions about

**static typing with type inference**:- What is type inference? Bring a really simple example that shows how type can be inferred.
- Why is static typing with type inference not dynamic typing? Use the definitions from lecture to explain your answer.
- What are some of the limitations of static typing with type inference? Provide an example that could be completed under dynamic typing, but not using static typing with type inference.

Answer these questions about

**currying**:What is currying? Create your own definition, and make sure it makes sense to someone who may have never written a computer program and only has high school level algebra (you may need to give an example, in terms of mathematics).

What is partial application of a function? How does currying make partial application simple?

Using any

*imperative*language which supports treating functions as data (e.g., JavaScript, Python, PHP, etc.), write the following function as a curried function. In other words, you should be able to call \(f(a, b, c)\) as \(f(a)(b)(c)\):\[f(a, b, c) = 2a^3 + b^2 - 3c + 4\]

Consider the following function, defined mathematically. \(f\) is a function which takes two arguments and produces one of the same type, and \(l = l_1,\ldots,l_n\) is a list of \(n\ (n > 1)\) elements of that type.

\[\begin{split}q(f, l) = \begin{cases} f(l_1, l_2) & \mbox{if } n = 2 \\ f(l_1, q(f, l_2\ldots{}l_n)) & \mbox{otherwise} \\ \end{cases}\end{split}\]What function \(f\) should be used so that this function will sum a sequence of numbers? How about for product?

Read Chapters 1 and 2 of LYH and prepare a tutorial for your learning group on how to use GHCi to preform simple arithmetic, load a Haskell file, and reload the current script.