% CSci 555: Functional Programming \
  Functional Programming in Scala \
  Functional Data Structures 
% **H. Conrad Cunningham**
% **6 March 2019**

---
lang: en
---

| Copyright (C) 2016, 2018, 2019,
  [H. Conrad Cunningham ](<http://www.cs.olemiss.edu/~hcc>)
| Professor of 
  [Computer and Information Science ](<https://www.cs.olemiss.edu>)
| [University of Mississippi ](<http://www.olemiss.edu>)
| 211 Weir Hall
| P.O. Box 1848
| University, MS 38677
| (662) 


**Note:** This set of notes accompanies my lectures on Chapter 3 of
the book *Functional Programming in Scala* \[Chiusano 2015\] (i.e. the
Red Book).

**Prerequisites**: In this set of notes, I assume the reader is
familiar with the programming concepts and Scala features covered in
my *Notes on Scala for Java Programmers* \[Cunningham 2019a\],
*Recursion Styles, Correctness, and Efficiency (Scala Version)*
\[Cunningham 2019b\], and *Type System Concepts* \[Cunningham 2019c\].

**Browser Advisory:** The HTML version of this textbook requires a
browser that supports the display of MathML. A good choice as of
March 2019 is a recent version of Firefox from Mozilla.


\newpage
\setcounter{section}{2}

# Functional Data Structures

## Introduction

To do functional programming, we construct programs from collections
of pure functions. Given the same arguments, a *pure function* always
returns the same result. The function application is thus
referentially transparent. By *referentially transparent* we mean that
a name or symbol always denotes the same value in some well-defined
context in the program.

Such a pure function does not have *side effects*. It does not modify
a variable or a data structure in place. It does not set throw an
exception or perform input/output. It does nothing that can be seen
from outside the function except return its value.

Thus the data structures in pure functional programs must be
*immutable*, not subject to change as the program executes.  (If
*mutable* data structures are used, no changes to the structures must be
detectable outside the function.)

For example, the Scala empty list---written as `Nil`{.scala} or
`List()`{.scala}---represents a value as immutable as the numbers
`2`{.scala} and `7`{.scala}.

Just as evaluating the expression `2 + 7`{.scala} yields a new number
`9`{.scala}, the concatenation of list `c`{.scala} and list
`d`{.scala} yields a new list (written `c ++ d`{.scala}) with the
elements of `c`{.scala} followed by the elements of `d`{.scala}. It
does not change the values of the original input lists `c`{.scala} and
`d`{.scala}.

Perhaps surprisingly, list concatenation does not require both lists
to be copied, as we see below.


## A `List` algebraic data type

To explore how to build immutable data structures in Scala, we examine
a simplified, singly linked list structure implemented as an algebraic
data type. This *list data type* is similar to the builtin Scala
`List`{.scala} data type.

What do we mean by algebraic data type?


### Algebraic data types

An *algebraic data type* is a type formed by combining other types,
that is, it is a *composite* data type. The data type is created by an
algebra of operations of two primary kinds:

-   a *sum* operation that constructs values to have one variant among
    several possible variants. These sum types are also called
    *tagged*, *disjoint union*, or *variant* types. 
    
    The combining operation is the alternation operator, which denotes
    the choice of one but not both between two alternatives.

-   a *product* operation that combines several values (i.e. *fields*)
    together to construct a single value. These are *tuple* and
    *record* types. 
    
    The combining operation is the Cartesian product from set theory.

We can combine sums and products recursively into arbitrarily large
structures.

An *enumerated type* is a sum type in which the constructors take no
arguments. Each constructor corresponds to a single value.


### ADT confusion

Although sometimes the acronym ADT is used for both, an *algebraic
data type* is a different concept from an *abstract data type*. 

-   We specify an algebraic data type with its *syntax* (i.e.
    structure)---with rules on how to compose and decompose them.
    
-   We specify an abstract data type with its *semantics* (i.e.
    meaning)---with rules about how the operations behave in relation
    to one another.
	
	TODO: Update paragraph below better for Scala course
    
    The modules we build with abstract interfaces, contracts, and data
    abstraction, such as the Rational Arithmetic modules from Chapter
    7 of reference \[Cunningham 2018\], are abstract data types.

Perhaps to add to the confusion, in functional programming we
sometimes use an algebraic data type to help define an abstract data
type.


### Using a Scala trait

A *list* consists of a sequence of values, all of which have the same
type.  It is a hierarchical data structure. It is either *empty* or
it is a pair consisting of a *head* element and a *tail* that is
itself a list of elements.

We define `List`{.scala} as an abstract type using a Scala
`trait`{.scala}. (We could also use an `abstract class`{.scala}
instead of a `trait`{.scala}.) We define the *constructors* for the
algebraic data type using the Scala `case class`{.scala} and 
`case object`{.scala} features.

~~~{.scala}
    sealed trait List[+A]
    case object Nil extends List[Nothing] 
    case class Cons[+A](head: A, tail: List[A]) extends List[A]
~~~

Thus `List`{.scala} is a *sum type* with two alternatives:

-   `Nil`{.scala} constructs the singleton case object that represents the
     empty list.

-   `Cons(h,t)`{.scala} constructs a new list from an element
    `h`{.scala}, called the *head*, and a list `t`{.scala}, called the
    *tail*.

`Cons`{.scala} itself is a *product (tuple) type* with two fields, one
of which is itself a `List`{.scala}.

The `sealed`{.scala} keyword tells the Scala compiler that all alternative
cases (i.e. subtypes) are declared in the current source file. No new
cases can be added elsewhere. This enables the compiler to generate
safe and efficient code for pattern matching.

As we have seen previously, for each `case class`{.scala} and 
`case object`{.scala}, the Scala compiler generates:

-   a constructor function (e.g. `Cons`{.scala})

-   accessor functions (methods) for each field (e.g. `head`{.scala}
    and `tail`{.scala} on `Cons`{.scala})
	
-   new definitions for `equals`{.scala}, `hashcode`{.scala}, and
    `toString`{.scala}

In addition, the `case object`{.scala} construct generates a
*singleton object*---a new type with exactly one instance.

Programs can use the constructors to build instances and use the
pattern matching to recognize the structure of instances and decompose
them for processing.

`List`{.scala} is a polymorphic type.  What does polymorphic mean? We
examine that in the next subsection.


#### Aside on Haskell

In Haskell, an algebraic data type similar to the Scala
`List[A]`{.scala} is the following:

~~~{.haskell}
    data List a = Nil | Cons a (List a)
                  deriving (Show, Eq)
~~~

The Haskell `List a`{.haskell} is a type similar to the Scala
`List[A]`. However, `Nil`{.scala} and `Cons[A]`{.scala} are subtypes
of `List`{.scala} in Scala, but they are not types in Haskell. In
Haskell, they are constructor functions that return values of type
`List a`{.haskell}.


### Polymorphism 

*Polymorphism* refers to the property of having "many shapes". In
programming languages, we are primarily interested in how
*polymorphic* function names (or operator symbols) are associated
with implementations of the functions (or operations).

Scala is a hybrid, object-functional language. Its type system
supports all three types of polymorphism discussed in the notes on
[*Type System Concepts* ](<../TypeConcepts/TypeSystemConcepts.html>).

-   *subtyping* by extending classes and traits

-   *parametric polymorphism* by using generic type parameters,

-   *overloading* through both the Java-like mechanisms and
    Haskell-like "type classes"

Scala's *type class pattern* builds on the languages's
`implicit`{.scala} classes and conversions. A type class enables a
programmer to enrich an existing class with an extended interface and
new methods without redefining the class or subclassing it.

For example, Scala extends the Java `String`{.scala} class (which is
`final`{.scala} and thus cannot be subclassed) with new features from
the `RichString`{.scala} wrapper class. The Scala `implicit`{.scala}
mechanisms associate the two classes "behind the scene". We defer
further discussion of implicits until later in the semester.

Note: The type class feature arose from the language Haskell. Similar
capabilities are called extension methods in C# and protocols in
Clojure and Elixir.

The `List`{.scala} data type defined above is polymorphic; it exhibits
both subtyping and parametric polymorphism. `Nil`{.scala} and
`Cons`{.scala} are subtypes of `List`{.scala}. The generic type
parameter `A`{.scala} denotes the type of the elements that occur in
the list.  For example, `List[Double]`{.scala} denotes a list of
double-precision floating point numbers.

What does the `+`{.scala} annotation mean in the definition
`List[+A]`{.scala}?



### Variance

The presence of both subtyping and parametric polymorphism leads to
the question of how these features interact---that is, the concept of
*variance*.


#### Covariance and contravariance

Suppose we have a supertype `Fish`{.scala} with a subtype
`Bass`{.scala}, which in turn has a subtype `BlackBass`{.scala}.

For generic data type `List[A]`{.scala} as defined above, consider
`List[Fish]`{.scala} and `List[Bass]`{.scala}.

-   If `List[Bass]`{.scala} is a subtype of `List[Fish]`{.scala},
    preserving the subtyping order, then the relationship is
    *covariant*.

-   If `List[Fish]`{.scala} is a subtype of `List[Bass]`{.scala},
    reversing the subtyping order, then the relationship is
    *contravariant*.

-   If there is no subtype relationship between `List[Fish]`{.scala}
    and `List[Bass]`{.scala}, the the relationship is *invariant*
    (sometimes called *nonvariant*).

In the Scala definition `List[+A]`{.scala} above, the `+`{.scala}
annotation in front of the `A`{.scala} is a *variance annotation*. The
`+`{.scala} means that parameter `A`{.scala} is a *covariant* parameter of
`List`{.scala}.  That is, for all types `X`{.scala} and `Y`{.scala} such that
`X`{.scala} is a subtype of `Y`{.scala}, then then `List[X]`{.scala}
is a is subtype of `List[Y]`{.scala}.

If we leave off the variance annotation, then `List`{.scala} would be
*invariant* in the type parameter. Regardless of how types `X`{.scala}
and `Y`{.scala} may be related, `List[X]`{.scala} and
`List[Y]`{.scala} are unrelated.

If we were put a `-`{.scala} annotation in front of `A`{.scala}, then
we declare parameter `A`{.scala} to be *contravariant*.  That is, for
all types `X`{.scala} and `Y`{.scala} such that `X`{.scala} is a
subtype of `Y`{.scala}, then then `List[Y]`{.scala} is a is subtype of
`List[X]`{.scala}.

In the definition of the `List`{.scala} algebraic data type,
`Nil`{.scala} extends `List[Nothing]`{.scala}. `Nothing`{.scala} is a
subtype of all other types. In conjunction with covariance, the
`Nil`{.scala} list can be considered a list of any type.

Aside: Note the position of `Nothing`{.scala} in Scala's 
[unified type hierarchy 
](<https://docs.scala-lang.org/tour/unified-types.html>).


#### Function subtypes

Now consider first-class functions. When is one function a subtype of
another? 

From the notes on
[*Type System Concepts* ](<../TypeConcepts/TypeSystemConcepts.html>)
\[Cunningham 2019c\], we recall that we should be able to safely
*substitute* elements of a subtype `S`{.scala} for elements of type
`T`{.scala} because `S`{.scala}'s operations behave the "same" as
`T`{.scala}'s operations. That is, the relationship satisfies the
*Liskov Substitution Principle* \[Liskov 1987\] \[Wikipedia 2019\].

Using the `Fish`{.scala} type hierarchy above, consider a function of
type `Bass => X`{.scala} (for some type `X`{.scala}). It would be
unsafe to use a function of type `BlackBass => X`{.scala} in its
place. The function would be undefined for any values that are of type
`Bass`{.scala} but are not of type `BlackBass`{.scala}. So a function
with a input type `BlackBass`{.scala} is not a subtype of a function
with input `Bass`{.scala}.

However, a function of type `Fish => X`{.scala} would be defined for
any value that is of type `Bass`{.scala}. So we need to examine the
relationships between the output types to determine what the subtyping
relationship is between the functions.

Consider a function of type `X => Bass`{.scala}. A function of type 
`X => BlackBass`{.scala} can be safely used in its place because a
`BlackBass`{.scala} is a `Bass`{.scala}, so the function yields a
value of the expected type.

However, a function of type `X => Fish`{.scala} cannot be safely used
in place of the `X => Bass`{.scala} function. It may yield some value
that is a `Fish`{.scala} but not a `Bass`{.scala}.

Thus we could safely use a `Bass => Bass`{.scala} function in place of
a `Bass => Fish`{.scala}, `BlackBass => Bass`{.scala}, or 
`BlackBass => Fish`{.scala} function.  Thus `Bass => Bass`{.scala} is a
subtype of the others.

However, we could not safely use a `Bass => Bass`{.scala} function in
place of a `Bass => BlackBass`{.scala}, 
`BlackBass => BlackBass`{.scala}, `Fish => Fish`{.scala}, 
`Fish => Bass`{.scala}, or `Fish => BlackBass`{.scala} function.
Thus `Bass => Bass`{.scala} is a not a subtype of the others.

Bringing these observations together, a function type 
`S1 => S2`{.scala} is a subtype of a function type `T1 => T2`{.scala}
if and only if:

-   `T1`{.scala} is a subtype of `S1`{.scala} (i.e. contravariant on the input type)

-   `S2`{.scala} is a subtype of `T2`{.scala} (i.e. covariant on the
    output type)
	
This general observation is consistent with the applicable theory.

For a Scala function of type `S => T`{.scala}, we thus say the
`S`{.scala} is in a *contravariant position* and `T`{.scala} is in a
*covariant position*.

TODO: May want to discuss multiargument functions.


### Defining functions in companion object

The *companion object* for a trait or class is a singleton object with
the same name as the trait or class. The companion object for the
`List`{.scala} trait is a convenient place to define functions for
manipulating the lists.

Because `List`{.scala} is a Scala algebraic data type (implemented
with case classes), we can use pattern matching in our function
definitions. Pattern matching helps enable the *form of the algorithm*
to match the *form of the data structure*. Or, in terms that Chiusano
and Bjarnason use, it helps in *following types to implementations*
\[Chiusano 2015\].

Note: Other writers call this design approach *type-driven
development* \[Brady 2017\] or *type-first development*
\[Petricek 2012\].

This is considered elegant. It is also pragmatic. The structure of the
data often suggests the algorithm needed for a task.

In general, lists have two cases that must be handled: the empty list
(represented by `Nil`{.scala}) and the nonempty list (represented by
`Cons`{.scala}). The first yields a *base leg* of a recursive
algorithm; the second yields a *recursive leg*.

Breaking a definition for a list-processing function into these two
cases is usually a good place to begin. We must ensure the recursion
*terminates*---that each successive recursive call gets closer to the
base case.


### Function to sum a list

Consider a function `sum`{.scala} to add together all the elements in
a list of integers. That is, if the list is $v_{1}, v_{2}, v_{3},
\cdots, v_{n}$, then the sum of the list is the value resulting from
inserting the addition operator between consecutive elements of the
list:

>   $v_{1} + v_{2} + v_{3} + \cdots + v_{n}$

Because addition is an *associative* operation, the additions can be
computed in any order.  That is, for any integers $x$, $y$, and $z$:

>   $(x + y) + z  =  x + (y + z)$ 

We can use the form of the data to guide the form of the
algorithm---or *follow the type to the implementation* of the
function.

What is the sum of an empty list?

Because there are no numbers to add, then, intuitively, zero seems to
be the proper value for the sum.

In general, if some binary operation is inserted between the elements
of a list, then the result for an empty list is the *identity element*
for the operation. Zero is the identity element for addition because,
for all integers $x$:

>   $0 + x = x = x + 0$

Now, how can we compute the sum of a nonempty list?

Because a nonempty list has at least one element, we can remove one
element and add it to the sum of the rest of the list. Note that the
"rest of the list" is a simpler (i.e. shorter) list than the original
list. This suggests a recursive definition.

The fact that we define lists recursively as a `Cons`{.scala} of a
head element with a tail list suggests that we structure the algorithm
around the structure of the *beginning* of the list.

Bringing together the two cases above, we can define the function
`sum`{.scala} in Scala using pattern matching as follows:

~~~{.scala}
    def sum(ints: List[Int]): Int = ints match {
		case Nil        => 0 
        case Cons(x,xs) => x + sum(xs)
	}
~~~

The length of a non-nil argument decreases by one for each successive
recursive application. Thus `sum`{.scala} will eventually be applied to a
`Nil`{.scala} argument and terminate.

For a list consisting of elements 2, 4, 6, and 8, that is,

~~~{.scala}
    Cons(2,Cons(4,Cons(6,Cons(8,Nil))))
~~~

function `sum`{.scala} computes:

~~~
    2 + (4 + (6 + (8 + 0)))
~~~

Function `sum`{.scala} is backward linear recursive; its time and space
complexity are both O($n$), where $n$ is the length of the input list.

We could, of course, redefine this to use a tail-recursive auxiliary
function.  With *tail call optimization*, the recursion could be
converted into a loop.  It would still be order O($n$)in time
complexity (but with a smaller constant factor) and O(1) space.


### Function to multiply a list

Now consider a function `product`{.scala} to multiply together a list
of floating point numbers.  The product of an empty list is 1 (which
is the identity element for multiplication). The product of a nonempty
list is the head of the list multiplied by the product of the tail of
the list, except that, if a 0 occurs anywhere in the list, the product
of the list is 0.  We can thus define `product`{.scala} with two bases
cases and one recursive case, as follows:

~~~{.scala}
    def product(ds: List[Double]): Double = ds match {
        case Nil          => 1.0
        case Cons(0.0, _) => 0.0
        case Cons(x,xs)   => x * product(xs)
    }
~~~

Note: 0 is the *zero element* for the multiplication operation on real
numbers.  That is, for all real numbers $x$:

>   $0 * x = x * 0 = 0$

For a list consisting of elements 2.0, 4.0, 6.0, and 8.0, that is,

~~~{.scala}
    Cons(2.0,Cons(4.0,Cons(6.0,Cons(8.0,Nil))))
~~~

function `product`{.scala} computes:

~~~{.scala}
    2.0 * (4.0 * (6.0 * (8.0 * 1.0))) 
~~~

For a list consisting of elements 2.0, 0.0, 6.0, and 8.0, function
`product`{.scala} "short circuits" the computation as:

~~~{.scala}
    2.0 * 0.0
~~~
  
Like `sum`{.scala}, function `product`{.scala} is backward linear
recursive; it has a worst-case time complexity of O($n$), where $n$ is
the length of the input list. It terminates because the argument of
each successive recursive call is one element shorter than the
previous call, approaching one of the base cases.


### Function to remove adjacent duplicates

Consider the problem of removing adjacent duplicate elements from a
list. That is, we want to replace a group of adjacent elements having
the same value by a single occurrence of that value.

As with the above functions, we let the form of the data guide the
form of the algorithm, following the type to the implementation.

The notion of adjacency is only meaningful when there are two or more of 
something. Thus, in approaching this problem, there seem to be three 
cases to consider:

-   The argument is a list whose first two elements are duplicates; in 
    which case one of them should be removed from the result. 

-   The argument is a list whose first two elements are not duplicates;
    in which case both elements are needed in the result. 

-   The argument is a list with fewer than two elements; in which case 
    the remaining element, if any, is needed in the result. 

Of course, we must be careful that sequences of more than two duplicates 
are handled properly. 

Our algorithm thus can examine the first two elements of the list. If 
they are equal, then the first is discarded and the process is repeated 
recursively on the list remaining. If they are not equal, then the first 
element is retained in the result and the process is repeated on the 
list remaining. In either case the remaining list is one element shorter 
than the original list. When the list has fewer than two elements, it is 
simply returned as the result. 

In Scala, we can define function `remdups`{.scala} as follows:

~~~{.scala}
    def remdups[A](ls: List[A]): List[A] = ls match {
        case Cons(x, Cons(y,ys)) =>
            if (x == y)
                remdups(Cons(y,ys))         // duplicate
            else
                Cons(x,remdups(Cons(y,ys))) // non-duplicate
        case _                   => ls
    }
~~~

Function `remdups`{.scala} puts the base case last in the pattern
match to take advantage of the wildcard match using `_`{.scala}.  This
needs to match either `Nil`{.scala} and `Cons(_,Nil)`{.scala}.
 
The function also depends upon the ability to compare any two elements
of the list for equality. Because `equals`{.scala} is builtin
operation on all types in Scala, we can define this function
polymorphically Without constraints on the type variable `A`{.scala}.

Like the previous functions, `remdups`{.scala} is backward linear
recursive; it takes a number of steps that is proportional to the
length of the list. This function has a recursive call on both the
duplicate and non-duplicate legs.  Each of these recursive calls uses
a list that is shorter than the previous call, thus moving closer to
the base case.


### Variadic function `apply`

We can also add a function `apply`{.scala} to the companion object
`List`{.scala}.

~~~{.scala}
    def apply[A](as: A*): List[A] = 
        if (as.isEmpty) 
		    Nil 
		else 
		    Cons(as.head, apply(as.tail: _*)) 
~~~

Scala treats an `apply`{.scala} method in an `object`{.scala}
specially.  We can invoke the `apply`{.scala} method using a postfix
`()`{.scala} operator. Given a singleton object `X`{.scala} with an
`apply`{.scala} method, the Scala complier translates the notation
`X(p)`{.scala} into the method call `X.apply(p)`{.scala}.

In the `List`{.scala} data type, function `apply`{.scala} is a
*variadic function*. It accepts zero or more arguments of type
`A`{.scala} as denoted by the type annotation `A*`{.scala} in the
parameter list. Scala collects these arguments into a `Seq`{.scala}
(sequence) data type for processing within the function. The special
syntax `_*`{.scala} reverses this and passes a sequence to another
function as variadic parameters.  Builtin Scala data structures such
as lists, queues, and vectors implement `Seq`{.scala}. It provides
methods such as the `isEmpty`{.scala}, `head`{.scala}, and
`tail`{.scala} methods used in `apply`{.scala}.

It is common to define a variadic `apply`{.scala} methods for
algebraic data types. This method enables us to create instances of
the data type conveniently. For example, `List(1,2,3)`{.scala} creates
a three-element list of integers with `1`{.scala} at the head.


## Data sharing

Suppose we have the declaration

~~~{.scala}
    val xs = Cons(1,Cons(2,Cons(3,Nil)))
~~~

or the more concise equivalent using the `apply`{.scala} method:

~~~{.scala}
    val xs = List(1,2,3)
~~~

As we learned in the data structures course, we can implement this
list as a linked list `xs`{.scala} with three cells with the values
`1`{.scala}, `2`{.scala}, and `3`{.scala}, as shown in Figure 3-1.

![**Figure 3-1: Data sharing in lists**](fig_03_01.png "Data sharing in lists") 

Consider the following declarations

~~~{.scala}
    val ys = Cons(0,xs)
    val zs = xs.tail
~~~

where

-   `Cons(0,xs)`{.scala} returns a list that has a new cell containing
    `0`{.scala} in front of the previous list

-   `xs.tail`{.scala} returns the list consisting of the last two
    elements of `xs`{.scala}
 
If the linked list `xs`{.scala} is immutable (i.e. the values and
pointers in the three cells cannot be changed), then neither of these
operations requires any copying.

-  The first just constructs a new cell containing `0`{.scala}, links
   it to the first cell in list `xs`{.scala}, and initializes
   `ys`{.scala} with a reference to the new cell.

-  The second just returns a reference to the second cell in
   list `xs`{.scala} and initializes `zs`{.scala} with this reference.

-  The original list `xs`{.scala} is still available, unaltered.

This is called *data sharing*.  It enables the programming language to
implement immutable data structures efficiently, without copying in
many key cases.

Also, such functional data structures are *persistent* because
existing references are never changed by operations on the data
structure.


### Function to take tail of list

Consider a function that takes a `List`{.scala} and returns its tail
`List`{.scala}. (This is different from the `tail`{.scala} accessor
method on `Cons`{.scala}.)

If the `List`{.scala} is a `Cons`{.scala}, then the function can
return the `tail`{.scala} element of the cell. What should it do if
the list is a `Nil`{.scala}?

There are several possibilities:

-   return `Nil`{.scala}

-   throw an exception (with perhaps a custom error string)

-   leave the function undefined in this case (which would result with a
    standard pattern match exception)

Generally speaking, the first choice seems misleading. It seems
illogical for an empty list to have a tail. And consider a typical
usage of the function.  It is normally an error for a program to
attempt to get the tail of an empty list. A program can efficiently
check whether a list is empty or not. So, in this case, it is probably
better to take the second or third approach.

We choose to implement `tailList`{.scala} so that it explicitly throws an
exception. It can be defined in the companion object for
`List`{.scala} as follows:

~~~{.scala}
    def tailList[A](ls: List[A]): List[A] = ls match {
        case Nil        => sys.error("tail of empty list")
        case Cons(_,xs) => xs
	}
~~~

Above, the value of the `head`{.scala} field of the `Cons`{.scala}
pattern is irrelevant in the computation on the right-hand side. There
is no need to introduce a new variable for that value, so we use the
wildcard variable `_`{.scala} to indicate that the value is not needed.

Function `tailList`{.scala} is O(1) in time complexity.  It does not
need to copy the list.  It is sufficient for it to just return a
reference to the tail of the original immutable list.  This return
value shares the data with its input argument.


### Function to drop from beginning of list

We can generalize `tailList`{.scala} to a function `drop`{.scala} that
removes the first `n`{.scala} elements of a list, as follows:

~~~{.scala}
    def drop[A](ls: List[A], n: Int): List[A] =
        if (n <= 0) ls
        else ls match {
            case Nil        => Nil
            case Cons(_,xs) => drop(xs, n-1)
	}
~~~

The `drop`{.scala} function terminates when either the list argument
is `Nil`{.scala} or the integer argument 0 or negative. The function
eventually terminates because each recursive call both shortens the
list and decrements the integer.

This function takes a different approach to the empty list issue than
`tailList`{.scala} does.  Although it seems illogical to take the
`tailList`{.scala} of an empty list, dropping the first element from an
empty list seems subtly different.  Given that we often use
`drop`{.scala} in cases where the length of the input list is unknown,
dropping the first element of an empty list does not necessarily
indicate a program error.

Suppose `drop`{.scala} throws an exception when called with an empty
list. To avoid this situation, the program might need to determine the
length of the list argument. This is inefficient, usually requiring a
traversal of the entire list to count the elements.


### Function to append lists

Consider the definition of an *append* (list concatenation)
function. We must define the `append`{.scala} function in terms of the
constructors `Nil`{.scala} and `Cons`{.scala}, already defined list
functions, and recursive applications of itself.

As with previous functions, we follow the type to the
implementation---let the form of the data guide the form of the
algorithm.

The `Cons`{.scala} constructor takes an element as its left operand and
a list as its right operand and returns a new list with the left
operand as the head and the right operand as the tail.

Similarly, append must take a list as its left operand and a list as its 
right operand and return a new list with the left operand as the initial 
segment and the right operand as the final segment. 

Given the definition of `Cons`{.scala}, it seems reasonable that an algorithm
for `append`{.scala} must consider the structure of its left operand. Thus we
consider the cases for nil and non-nil left operands.

-   If the left operand is `Nil`{.scala}, then the function can just return the
    right operand.

-   If the left operand is a `Cons`{.scala} (that is, non-nil), then
    the result consists of the left operand’s head followed by the
    append of the left operand’s tail to the right operand.

In following the type to the implementation, we use the form of the
left operand in a pattern match. We define `append`{.scala} as
follows:

~~~{.scala}
    def append[A](ls: List[A], rs: List[A]): List[A] = ls match {
        case Nil        => rs
        case Cons(x,xs) => Cons(x, append(xs, rs))
    }
~~~

For the recursive application of `append`{.scala}, the length of the
left operand decreases by one. Hence the left operand of an
`append`{.scala} application eventually becomes `Nil`{.scala},
allowing the evaluation to terminate.

The number of steps needed to evaluate `append(as,bs)`{.scala} is
proportional to the length of `as`{.scala}, the left operand. That is,
it is O($n$), where $n$ is the length of list `as`.

Moreover, `append(as,bs)`{.scala} only needs to copy the list
`as`{.scala}.  The list `bs`{.scala} is shared between the second
operand and the result. If we did a similar function to append two
(mutable) arrays, we would need to copy both input arrays to create
the output array. Thus, in this case, a linked list is more efficient
than arrays!

The append operation has a number of useful mathematical (algebraic)
properties, for example, associativity and an identity element.

Associativity---For any finite lists `xs`{.scala}, `ys`{.scala}, and
`zs`{.scala}:

~~~{.scala}
    append(xs,append(ys,zs)) = append(append(xs,ys),zs)
~~~

Identity---For any finite list `xs`{.scala}:

~~~{.scala}
    append(Nil,xs) = append(xs,Nil) = xs
~~~

Scala's builtin `List`{.scala} type uses the infix operator
`++`{.scala} for the "append" operation. For this operator,
associativity can be stated conveniently with the equation:

~~~{.scala}
    xs ++ (ys ++ zs) = (xs ++ ys) ++ zs
~~~

Mathematically, the `List`{.scala} data type and the binary operation
`append`{.scala} form a kind of abstract algebra called a
*monoid*. Function `append`{.scala} is closed (i.e. it takes two lists
and gives a list back), is associative, and has an identity element.

Aside: For more information on operations and algebraic structures,
see Chapter 80,
["Review of Relevant Mathematics" 
](<https://john.cs.olemiss.edu/~hcc/csci450/ELIFP/Ch80/80\_Math\_Review.html>),
in the Haskell-based book
[*Exploring Languages with Interpreters and Functional Programming* ](<https://john.cs.olemiss.edu/~hcc/csci450/ELIFP/ExploringLanguages.html>)
\[Cunningham 2018\]. For discussion of how to prove properties like
those above, see Chapter 25, ["Proving Haskell Laws" 
](<https://john.cs.olemiss.edu/~hcc/csci450/ELIFP/Ch25/25_Laws.html>),
in the same book.


## Other list functions

### Tail recursive function reverse

Consider the problem of reversing the order of the elements in a list.

Again we can use the structure of the data to guide the algorithm
development. If the argument is a nil list, then the function returns
a nil list. If the argument is a non-nil list, then the function can
append the head element at the back of the reversed tail.

~~~{.scala} 
    def rev[A](ls: List[A]): List[A] = ls match {
        case Nil        => Nil
        case Cons(x,xs) => append(rev(xs),List(x))
    }
~~~

Given that evaluation of `append`{.scala} terminates, the evaluation
of `rev`{.scala} also terminates because all recursive applications
decrease the length of the argument by one.

How efficient is this function?

The evaluation of `rev`{.scala} takes O($n^{2}$) steps, where $n$ is
the length of the argument. There are O($n$) applications of
`rev`{.scala} . For each application of `rev`{.scala} there are O($n$)
applications of `append`{.scala} .

The initial list and its reverse do not share data.

Function `rev`{.scala} has a number of useful properties, for example
a distribution and an inverse properties.

Distribution---For any finite lists `xs`{.scala}  and `ys`{.scala}:

~~~{.scala}
    rev(append(xs,ys)) = append(rev(ys), rev(xs))
~~~

Inverse---For any finite list `xs`{.scala}:

~~~{.scala}
    rev(rev(xs)) = xs
~~~

Can we define a function to reverse a list using a "more efficient"
tail recursive solution?

As we have seen, a common technique for converting a backward linear
recursive definition like `rev`{.scala} into a *tail recursive*
definition is to use an *accumulating parameter* to build up the
desired result incrementally. A possible definition for a tail
recursive auxiliary function is:

~~~{.scala}
    def revAux[A](ls: List[A], as: List[A]): List[A] = ls match {
        case Nil        => as
        case Cons(x,xs) => revAux(xs,Cons(x,as))
    }
~~~

In this definition parameter `as`{.scala} is the accumulating
parameter. The head of the first argument becomes the new head of the
accumulating parameter for the tail recursive call. The tail of the
first argument becomes the new first argument for the tail recursive
call.

We know that `revAux`{.scala} terminates because, for each recursive
application, the length of the first argument decreases toward the
base case of `Nil`{.scala}.

We note that `rev(xs)`{.scala} is equivalent to
`revAux(xs,Nil)`{.scala} .

To define a single-argument replacement for `rev`{.scala} , we can
embed the definition of `revAux’`{.scala} as an *auxiliary* function
within the definition of a new function `reverse`{.scala} .

~~~{.scala} 
    def reverse[A](ls: List[A]): List[A] = {
        def revAux[A](rs: List[A], as: List[A]): List[A] = 
		    rs match {
                case Nil        => as
                case Cons(x,xs) => revAux(xs,Cons(x,as))
            }
        revAux(ls,Nil)
    }
~~~

Function `reverse(xs)`{.scala} returns the value from
`revAux(xs,Nil)`{.scala}.

How efficient is this function?

The evaluation of `reverse`{.scala} takes O($n$) steps, where $n$ is
the length of the argument. There is one application of
`revAux`{.scala} for each element; `revAux`{.scala} requires a single
O(1) `Cons`{.scala} operation in the accumulating parameter.

Where did the increase in efficiency come from?

Each application of `rev`{.scala} applies `append`{.scala}, a linear
time (i.e.  O($n$)) function. In `revAux`, we replaced the
applications of `append`{.scala} by applications of `Cons`{.scala}, a
constant time (i.e. O(1)) function.

In addition, a compiler or interpreter that does tail call optimization
can translate this tail recursive call into a loop on the host machine.


### Higher-order function dropWhile

Consider a function `dropWhile`{.scala} that removes elements from the
front of a `List`{.scala} while its predicate argument (a Boolean
function) holds.

~~~{.scala}
    def dropWhile [A](ls: List[A], f: A => Boolean): List[A] =
        ls match {
            case Cons(x,xs) if f(x) => dropWhile(xs, f)
            case _                  => ls
        }
~~~

This higher-order function terminates when either the list is empty or
the head of the list makes the predicate false. For each successive
recursive call, the list argument is one element shorter than the
previous call, so the function eventually terminates.

If evaluation of function argument `p`{.scala} is O(1), then function
`dropWhile`{.scala} has worst-case time complexity O($n$), where $n$
is the length of its first operand. The result list shares data with
the input list.


### Curried function dropWhile

We often pass *anonymous functions*  to higher-order utility functions
like `dropwhile`{.scala}, which has the signature:

~~~{.scala}
    def dropWhile[A](ls: List[A], f: A => Boolean): List[A]
~~~

When we call `dropWhile`{.scala} with an anonymous function for
`f`{.scala}, we must specify the type of its argument, as follows:

~~~{.scala}
    val xs: List[Int] = List(1,2,3,4,5)
    val ex1 = dropWhile(xs, (x: Int) => x < 4)
~~~

Even though it is clear from the first argument that higher order
argument `f`{.scala} must take an integer as its argument, the Scala
*type inference* mechanism cannot detect this.

However, if we rewrite `dropWhile`{.scala} in the following form, type
inference can work as we want:

~~~{.scala}
    def dropWhile2[A](ls: List[A])(f: A => Boolean): List[A] =
        ls match {
            case Cons(x,xs) if f(x) => dropWhile2(xs)(f)
            case _                  => ls
		}
~~~

Function `dropWhile2`{.scala} is written in *curried* form above. In
this form, a function that takes two arguments can be represented as a
function that takes the first argument and returns a function, which
itself takes the second argument.

If we apply `dropWhile2`{.scala} to just the first argument, we get a
function. We call this a *partial application* of
`dropWhile2`{.scala}.

More generally, a function that takes multiple arguments can be
represented by a function that takes its arguments in groups of one or
more from left to right. If the function is partially applied to the
first group, it returns a function that takes the remaining groups,
and so forth.

Currying and partial application are directly useful in a number of
ways in our programs. Here currying is indirectly useful by assisting
type inference. If a function is defined with multiple groups of
arguments, the type information flows from one group to another, left
to right.  In `dropWhile2`{.scala}, the first argument group binds
type variable `A`{.scala} to `Int`{.scala}.  Then this binding can be
used in the second argument group.


## Generalizing to Higher Order Functions

### Fold Right 

Consider the `sum`{.scala} and `product`{.scala} functions we defined
above, ignoring the short-cut handling of the zero element in
`product`{.scala}.

~~~{.scala}
    def sum(ints: List[Int]): Int = ints match {
		case Nil        => 0 
        case Cons(x,xs) => x + sum(xs)
	}

    def product(ds: List[Double]): Double = ds match {
        case Nil          => 1.0
        case Cons(x,xs)   => x * product(xs)
    }
~~~

What do `sum`{.scala} and `product`{.scala} have in common? What
differs?

Both exhibit the same *pattern of computation*.

-   Both take a list as input.

    But the element type differs. Function `sum`{.scala} takes a
    list of `Int`{.scala} values and `product`{.scala} takes a list
    of `Double`{.scala} values.
 	
-   Both insert a binary operator between all the consecutive elements
    of the list in order to reduce the list to a single value.
	
	But the binary operation differs. Function `sum`{.scala} applies
    integer addition and `product`{.scala} applies double-precision
    floating-point multiplication.

-   Both group the operations from the right to the left. 

-   Both functions return some value for an empty list. The function
    extends nonempty input lists to implicitly include this value as
    the "rightmost" value of the input list.
	
	But the actual value differs.

    Function `sum`{.scala} returns integer 0, the (right) identity
    element for addition.

    Function `product`{.scala} returns 1.0, the (right) identity
    element for multiplication.
	
	In general, this value could be something other than the (right)
    identity element.
	
-   Both return a value of the same element type as the input list.

    But the input type differs, as we noted above.

Both functions insert operations of type `(A,A) => A`{.scala} between
elements a list of type `List[A]`{.scala}, for some generic type
`A`{.scala}.

But these are special cases of more general operations of type 
`(A,B) => B`{.scala}. In this case, the value returned must be of
type `B`{.scala} in the case of both empty and nonempty lists.

Whenever we recognize a pattern like this, we can systematically
*generalize the function* definition as follows:


1.  Do a *scope-commonality-variability* (SCV) analysis on the set of
    related functions.

    That is, identify what is to be included and what not (i.e. the
    *scope*), the parts of functions that are the same (the
    *commonalities* or *frozen spots*), and the parts that differ (the
    *variabilities* or *hot spots*).


2.  Leave the commonalities in the generalized function's body.


3.  Move the variabilities into the generalized function’s header---its
    type signature and parameter list.

    -   If the part moved to the generalized function’s parameter list
        is an expression, then make that part a function with a
        parameter for each local variable accessed.

    -   If a data type potentially differs from a specific type used
        in the set of related functions, then add a type parameter to
        the generalized function.

    -   If the same data value or type appears in multiple roles, then
        consider adding distinct type or value parameters for each
        role.
		
4.  Consider other approaches if the generalized function’s type
    signature and parameter list become too complex.

    For example, we can introduce new data or procedural abstractions
    for parts of the generalized function. These may be in the same
    "module" (i.e. object, class, etc.) as the generalized function,
    in an appropriately defined separate "module" that is imported,
    etc. A separate module may better accomplish the desired
    parameterization of the function.

A similar approach can be used to generalize a whole class.

Following the above guidelines, we can express the common pattern from
`sum`{.scala} and `product`{.scala} as a new (broadly useful) polymorphic,
higher-order function `foldRight`{.scala}, which we define as follows:

~~~{.scala}
    def foldRight[A,B](ls: List[A], z: B)(f: (A, B) => B): B = 
        ls match {
            case Nil        => z
            case Cons(x,xs) => f(x, foldRight(xs, z)(f))
        }
~~~

This function:

-   passes in the binary operation `f`{.scala} that combines the list
    elements

-   passes in the element `z`{.scala} to be returned for empty lists
    (often the right identity element for the operation, but this is
    not required)
	
-   uses two type parameters `A`{.scala} and `B`{.scala}---one for the
    type of elements in the list and one for the type of the result

The `foldRight`{.scala} function "folds" the list elements (of type
`A`{.scala}) into a value (of type `B`{.scala}) by "inserting"
operation `f`{.scala} between the elements, with value `z`{.scala}
"appended" as the rightmost element.  For example,
`foldRight(List(1,2,3),z)(f)`{.scala} expands to
`f(1,f(2,f(3,z)))`{.scala}.
  
Function `foldRight`{.scala} is not tail recursive, so it needs a new
stack frame for each element of the input list. If its list argument
is long or the folding function itself is expensive, then the function
can terminate with a *stack overflow* error.
 
We can specialize `foldRight`{.scala} to have the same functionality
as `sum`{.scala} and `product`{.scala}.

~~~{.scala}
    def sum2(ns: List[Int]) =
        foldRight(ns, 0)((x,y) => x + y)

    def product2(ns: List[Double]) =
        foldRight(ns, 1.0)(_ * _)
~~~

The expression `(_ * _)`{.scala} in `product2`{.scala} is a concise
notation for the anonymous function `(x,y) => x * y`{.scala}.  The two
underscores denote two distinct anonymous variables.  This concise
notation can be used in a context where Scala's type inference
mechanism can determine the types of the anonymous variables.
]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
We can construct a recursive function to compute the length of a
polymorphic list. However, we can also express this computation using
`foldRight`{.scala}, as follows:

~~~{.scala}
    def length[A](ls: List[A]): Int =
        foldRight(ls, 0)((_,acc) => acc + 1)
~~~

We use the `z`{.scala} parameter to accumulate the count, starting it
at 0. Higher order argument `f`{.scala} is a function that takes an
element of the list as its left argument and the previous accumulator
as its right argument and returns it incremented by 1. In this
application, `z`{.scala} is not the identity element for `f`{.scala}
by a convenient beginning value for the counter.

We can construct an "append" function that uses `foldRight`{.scala} as
follows:

~~~{.scala}
    def append2[A](ls: List[A], rs: List[A]): List[A] =
        foldRight(ls, rs)(Cons(_,_))
~~~

Here the the list that `foldRight`{.scala} operates on the first
argument of the append. The `z`{.scala} parameter is the entire second
argument and the combining function is just `Cons`.  So the effect is
to replace the `Nil`{.scala} at the end of the first list by the
entire second list.

We can construct a recursive function that takes a list of lists and
returns a "flat" list that has the same elements in the same order.
We can also express this `concat`{.scala} function in terms of
`foldRight`{.scala}, as follows:

~~~{.scala}
    def concat[A](ls: List[List[A]]): List[A] =
        foldRight(ls, Nil: List[A])(append) 
~~~

Function `append`{.scala} takes time proportional to the length of its
first list argument. This argument does not grow larger because of
right associativity of `foldRight`{.scala}. Thus `concat`{.scala}
takes time proportional to the total length of all the lists.

Above, we "pass" the `append`{.scala} function without writing an
explicit anonymous function definition (i.e. *function literal*) such
as `(xs,ys) => append(xs,ys)`{.scala} or `append(_,_)`{.scala}.

In `concat`{.scala}, for which Scala can infer the types of
`append`{.scala}'s arguments, the compiler can generate the needed
function literal. In other cases, we would need to use *partial
application* notation such as

~~~{.scala}
    append _
~~~

or an explicit function literal such as 

~~~{.scala}
    (xs: List[A], ys: List[A]) => append(xs,ys)
~~~

to enable the compiler to infer the types.

Above we defined function `foldRight`{.scala} as a backward recursive
function that processes the elements of a list one by one. However, as
we have seen, it is often more useful to think of `foldRight`{.scala}
as a powerful list operator that reduces the element of the list into
a single value. We can combine `foldRight`{.scala} with other
operators to conveniently construct list processing programs.


### Fold Left

We designed function `foldRight`{.scala} above as a backward linear
recursive function with the signature:

~~~{.scala}
    foldRight[A,B](as: List[A], z: B)(f: (A, B) => B): B
~~~

As noted:

~~~{.scala}
    foldRight(List(1,2,3),z)(f) == f(1,f(2,f(3,z)))
~~~

Consider a function `foldLeft`{.scala} such that:

~~~{.scala}
    foldLeft(List(1,2,3),z)(f) == f(f(f(z,1),2),3)
~~~

This function folds from the left.  It offers us the opportunity to
use parameter `z`{.scala} as an accumulating parameter in a tail
recursive implementation, as follows:

~~~{.scala}
    @annotation.tailrec
    def foldLeft[A,B](ls: List[A], z: B)(f: (B, A) => B): B = 
	    ls match {
            case Nil        => z
            case Cons(x,xs) => foldLeft(xs, f(z,x))(f)
        }
~~~

In the first line above, we *annotate* function `foldLeft`{.scala} as
tail recursive using `@annotation.tailrec`{.scala}.  If the function
is not tail recursive, the compiler gives an error, rather than
silently generating code that does not use tail call optimization
(i.e. does not convert the recursion to a loop).

We can implement list sum, product, and length functions with
`foldLeft`{.scala}, similar to what we did with `foldRight`{.scala}.

~~~{.scala}
    def sum3(ns: List[Int]) =
        foldLeft(ns, 0)(_ + _) 
		
    def product3(ns: List[Double]) =
		foldLeft(ns, 1.0)(_ * _)
~~~

Given that addition and multiplication of numbers are associative and
have identity elements, `sum3`{.scala} and `product3`{.scala} use the
same values for parameters `z`{.scala} and `f`{.scala} as
`foldRight`{.scala}.

Function `length2`{.scala} that uses `foldLeft`{.scala} is like
`length`{.scala} except that the arguments of function `f`{.scala} are
reversed.

~~~{.scala}
    def length2[A](ls: List[A]): Int =
        foldLeft(ls, 0)((acc,_) => acc + 1)
~~~

We can also implement list reversal using `foldLeft`{.scala} as
follows:

~~~{.scala}
    def reverse2[A](ls: List[A]): List[A] =
        foldLeft(ls, List[A]())((acc,x) => Cons(x,acc))
~~~

This gives a solution similar to the tail recursive `reverse`{.scala}
function above. The `z`{.scala} value is initially an empty list; the
folding function `f`{.scala} uses `Cons`{.scala} to "attach" each
element of the list to front of the accumulator, incrementally
building the list in reverse order.

Because `foldLeft`{.scala} is tail recursive and `foldRight`{.scala}
is not, `foldLeft`{.scala} is usually safer and more efficient to use
in than `foldRight`{.scala}. (If the list argument is lazily evaluated
or the function argument `f`{.scala} is nonstrict in at least one of
its arguments, then there are other factors to consider. We will
discuss what we mean by "lazily evaluated" and "nonstrict" later in
the course.)

To avoid the stack overflow situation with `foldRight`{.scala}, we can
first apply `reverse`{.scala} to the list argument and then apply
`foldLeft`{.scala} as follows:

~~~{.scala}
    def foldRight2[A,B](ls: List[A], z: B)(f: (A,B) => B): B =
        foldLeft(reverse(ls), z)((b,a) => f(a,b))
~~~

The combining function in the call to `foldLeft`{.scala} is the same
as the one passed to `foldRight2`{.scala} except that its arguments
are reversed.


### Map

Consider the following two functions, noting their type signatures and
patterns of recursion.

The first, `squareAll`{.scala}, takes a list of integers and returns
the corresponding list of squares of the integers.

~~~{.scala}
    def squareAll(ns: List[Int]): List[Int] = ns match {
        case Nil         => Nil
        case Cons(x, xs) => Cons(x*x, squareAll(xs))
    } 
~~~

The second, `lengthAll`{.scala}, takes a list of lists and returns the
corresponding list of the lengths of the element lists

~~~{.scala}
    def lengthAll[A](lss: List[List[A]]): List[Int] =
        lss match {
            case Nil           => Nil
            case Cons(xs, xss) => Cons(length(xs),lengthAll(xss))
        }
~~~

Although these functions take different kinds of data (a list of
integers versus a list of polymorphically typed lists) and apply
different operations (squaring versus list length), they exhibit the
same pattern of computation. That is, both take a list and apply
some function to each element to generate a resulting list of the same
size as the original.

As with the fold functions, the combination of polymorphic typing and
higher-order functions allows us to abstract this pattern of
computation into a higher-order function.

We can abstract the pattern of computation common to
`squareAll`{.scala} and `lengthAll`{.scala} as the (broadly useful)
function `map`{.scala}, defined as follows:

~~~{.scala}
    def map[A,B](ls: List[A])(f: A => B): List[B] = ls match {
        case Nil        => Nil
        case Cons(x,xs) => Cons(f(x),map(xs)(f))
    }
~~~

Function `map`{.scala} takes a list of type `A`{.scala} elements,
applies function `f`{.scala} of type `A => B`{.scala} to each element,
and returns a list of the resulting type `B`{.scala} elements.

Thus we can redefine `squareAll`{.scala} and `lengthAll`{.scala} using
`map`{.scala} as follows:

~~~{.scala}
    def squareAll2(ns: List[Int]): List[Int] =
        map(ns)(x => x*x)
		
    def lengthAll2[A](lss: List[List[A]]): List[Int] =
		map(lss)(length)
~~~

We can implement `map`{.scala} itself using `foldRight`{.scala} as
follows:

~~~{.scala}
    def map1[A,B](ls: List[A])(f: A => B): List[B] =
        foldRight(ls, Nil: List[B])((x,xs) => Cons(f(x),xs))
~~~

The folding function `(x,xs) => Cons(f(x),xs)`{.scala} applies the
mapping function `f`{.scala} to the next element of the list (moving
right to left) and attaches the result on the front of the processed
tail.

As implemented above, function `map`{.scala} is backward recursive; it
thus requires a stack frame for each element of its list argument.
For long lists, the recursion can cause a stack overflow
error. Function `map1`{.scala} uses `foldRight`{.scala}, which has
similar characteristics.  So we need to use these functions with
care. However, we can use the reversal technique illustrated in
`foldRight2`{.scala} if necessary.

We could also optimize function `map`{.scala} using *local
mutation*. That is, we can use a mutable data structure within the
`map`{.scala} function but not allow this structure to be accessed
outside of `map`{.scala}.  The following function takes that approach,
using a `ListBuffer`{.scala}:

~~~{.scala}
    def map2[A,B](ls: List[A])(f: A => B): List[B] = {
        val buf = new collection.mutable.ListBuffer[B]

        @annotation.tailrec
        def go(ls: List[A]): Unit = ls match {
            case Nil        => ()
            case Cons(x,xs) => buf += f(x); go(xs)
        }
    
        go(ls)
        List(buf.toList: _*) 
    }
~~~

A `ListBuffer`{.scala} is a mutable list data structure from the Scala
library.  The operation `+=`{.scala} appends a single element to the
end of the buffer in constant time. The method `toList`{.scala}
converts the `ListBuffer`{.scala} to a Scala immutable list, which is
similar to the data structure we are developing in this module.


### Filter

Consider the following two functions.

The first, `getEven`{.scala}, takes a list of integers and returns the
list of those integers that are even (i.e. are multiples of 2). The
function preserves the relative order of the elements in the list.

~~~{.scala}
    def getEven(ns: List[Int]): List[Int] = ns match {
        case Nil        => Nil
        case Cons(x,xs) =>
            if (x % 2 == 0)  // divisible evenly by 2
                Cons(x,getEven(xs))
            else
                getEven(xs)
    }
~~~

The second, `doublePos`{.scala}, takes a list of integers and returns
the list of doubles of the positive integers from the input list; it
preserves the order of the elements.

~~~{.scala}
    def doublePos(ns: List[Int]): List[Int]  = ns match {
        case Nil        => Nil
        case Cons(x,xs) =>
            if (0 < x)
                Cons(2*x, doublePos(xs))
            else
                doublePos(xs)
    }			
~~~

We can abstract the pattern of computation common to `getEven`{.scala}
and `doublePos`{.scala} as the (broadly useful) function
`filter`{.scala}, defined as follows:

~~~{.scala}
    def filter[A](ls: List[A])(p: A => Boolean): List[A] =
        ls match {
            case Nil        => Nil
            case Cons(x,xs) =>
			    val fs = filter(xs)(p)
			    if (p(x)) Cons(x,fs) else fs
        }
~~~

Function `filter`{.scala} takes a predicate `p`{.scala} of type 
`A => Boolean`{.scala} a list of type `List[A]`{.scala} and returns a
list containing those elements that satisfy `p`{.scala}, in the same
order as the input list.

Therefore, we can redefine `getEven`{.scala} and `doublePos`{.scala}
as follows:

~~~{.scala}
    def getEven2(ns: List[Int]): List[Int] =
        filter(ns)(x => x % 2 == 0)

    def doublePos2(ns: List[Int]): List[Int] =
        map(filter(ns)(x => 0 < x))(y => 2 * y)
~~~

Function `doublePos2`{.scala} exhibits both the `filter`{.scala} and
the `map`{.scala} patterns of computation.

The higher-order functions `map`{.scala} and `filter`{.scala} allowed
us to restate the definitions of `getEven`{.scala} and
`doublePos`{.scala} in a succinct form.

We can implement `filter`{.scala} in terms of `foldRight`{.scala} as
follows:

~~~{.scala}
    def filter1[A](ls: List[A])(p: A => Boolean): List[A] =
        foldRight(ls, Nil:List[A])((x,xs) => if (p(x)) Cons(x,xs) else xs)
~~~

Above, the folding function 

~~~{.scala}
    (x,xs) => if (p(x)) Cons(x,xs) else xs
~~~

applies the filter predicate `p`{.scala} to the next element of the
list (moving right to left). If the predicate evaluates to true, the
folding function attaches that element on the front of the processed
tail; otherwise, it omits the element from the result.


### Flat Map

The higher-order function `map`{.scala} applies its function argument
`f`{.scala} to every element of a list and returns the list of
results. If the function argument `f`{.scala} returns a list, then the
result is a list of lists.  Often we wish to flatten this into a
single list, that is, apply a function like `concat`{.scala} defined
in a previous section.

This computation is sufficiently common that we give it the name
`flatMap`{.scala}.  We can define it in terms of `map`{.scala} and
`concat`{.scala} as

~~~{.scala}
    def flatMap[A,B](ls: List[A])(f: A => List[B]): List[B] =
        concat(map(ls)(f))
~~~

or by combining `map`{.scala} and `concat`{.scala} into one
`foldRight`{.scala} as:

~~~{.scala}
    def flatMap1[A,B](ls: List[A])(f: A => List[B]): List[B] =
        foldRight(ls, Nil: List[B])(
            (x: A, ys: List[B]) => append(f(x),ys))
~~~

Above, the function argument to `foldRight`{.scala} applies the
`flatMap`{.scala} function argument `f`{.scala} to each element of the
list argument and then appends the resulting list in front of the
result from processing the elements to the right.

We can also define `filter`{.scala} in terms of `flatMap`{.scala} as
follows:

~~~{.scala}
    def filter2[A](ls: List[A])(p: A => Boolean): List[A] =
        flatMap(ls)(x => if (p(x)) List(x) else Nil)
~~~

The function argument to `flatMap`{.scala} generates a one-element
list if the filter predicate `p`{.scala} is true and an empty list if
it is false.


## Classic algorithms on lists


### Insertion sort and bounded generics

Consider a function to sort the elements of a list into ascending
order. A simple algorithm to do this is *insertion sort*. To sort a
non-empty list with head `x`{.scala} and tail `xs`{.scala}, sort the
tail `xs`{.scala} and insert the element `x`{.scala} at the right
position in the result. To sort an empty list, just return it.

If we restrict the function to integer lists, we get the following
Scala functions:

~~~{.scala}
    def isort(ls: List[Int]): List[Int] = ls match {
        case Nil        => Nil
        case Cons(x,xs) => insert(x,isort(xs))
    }

    def insert(x: Int, xs: List[Int]): List[Int] = xs match {
        case Nil        => List(x)
        case Cons(y,ys) =>
            if (x <= y)
                Cons(x,xs)
            else
                Cons(y,insert(x,ys))
    }
~~~

Insertion sort has a (worst and average case) time complexity of
O($n^{2}$) where $n$ is the length of the input list. (Function
`isort`{.scala} requires $n$ consecutive recursive calls; each call
uses function `insert`{.scala} which itself requires on the order of
$n$ recursive calls.)

Now suppose we want to generalize the sorting function and make it
polymorphic.  We cannot just add a type parameter `A`{.scala} and
substitute it for `Int`{.scala} everywhere.  Although all Scala data
types support equality and inequality comparison, not all types can be
compared on a *total ordering* (`<`{.scala}, `<=`{.scala},
`>`{.scala}, and `>=`{.scala} as well).

Fortunately, the Scala library provides a trait `Ordered`{.scala}. Any
class that provides the other comparisons can extend this trait; the
standard types in the library do so. This trait adds the comparison
operators as methods so that they can be called in infix form.

~~~{.scala}
    trait Ordered[A] {
        def compare(that: A): Int
        def < (that: A): Boolean = (this compare that) <  0
        def > (that: A): Boolean = (this compare that) >  0
        def <=(that: A): Boolean = (this compare that) <= 0
        def >=(that: A): Boolean = (this compare that) >= 0
		def compareTo(that: a) = compare(that)
    }
~~~

We thus need to restrict the polymorphism on `A`{.scala} to be a
subtype of `Ordered[A]`{.scala} by putting an *upper bound* on the
type as follows:

~~~{.scala}
    def isort[A <: Ordered[A]](ls: List[A]): List[A]
~~~

Note: In addition to upper bounds, we can use a *lower bound*. A
constraint `A :> T`{.scala} requires type `A`{.scala} to be a
supertype of type `T`{.scala}.  We can also specify both an upper and
a lower bound on a type such as `T1 <: A <: T2`{.scala},

By using the upper bound constraint, we can sort data from any type
that extends `Ordered`{.scala}.  However, the primitive types
inherited from Java do not extend `Ordered`{.scala}.

Fortunately, the Scala library defines implicit conversions between
the Java primitive types and Scala's enriched wrapper types. (This is
the "type class" mechanism we discussed earlier.) We can use a weaker
*view bound* constraint, denoted by `<%`{.scala} instead of
`<:`{.scala}. This `A`{.scala} to be any type that is a subtype of or
convertible to `Ordered[A]`{.scala}.

~~~{.scala}
    def isort1[A <% Ordered[A]](ls: List[A]): List[A] = 
	    ls match {
            case Nil        => Nil
            case Cons(x,xs) => insert1(x,isort1(xs))
        }

    def insert1[A <% Ordered[A]](x: A, xs: List[A]): List[A] =
        xs match {
            case Nil        => List(x)
            case Cons(y,ys) =>
                if (x <= y)
                    Cons(x,xs)
                else
                    Cons(y,insert1(x,ys))
        }
~~~

We could define `insert`{.scala} inside `isort`{.scala} and avoid the
separate type parameterization. But `insert`{.scala} is separately
useful, so it is reasonable to leave it external.

An alternative to use of the bound would be to pass in the needed
comparison predicate, as follows:

~~~{.scala}
    def isort2[A](ls: List[A])(leq: (A,A) => Boolean): List[A] =
        ls match {
            case Nil        => Nil
            case Cons(x,xs) => insert2(x,isort2(xs)(leq))(leq)
        }

    def insert2[A](x:A, xs:List[A])(leq:(A,A)=>Boolean):List[A] =
        xs match {
            case Nil        => List(x)
            case Cons(y,ys) =>
                if (leq(x,y))
                    Cons(x,xs)
                else
                    Cons(y,insert2(x,ys)(leq))
        }
~~~

Above we expressed both functions in curried form.  By putting the
comparison function last, we enabled the compiler to infer the
argument types for the function.

If we placed the function in the first argument group, the user of the
function would have to supply the types.  However, putting the
comparison function first might allow a more useful partial
application of the `isort`{.scala} to a comparison function.


### Merge sort

The insertion sort given in the previous section has an average case
time complexity of O($n^{2}$) where $n$ is the length of the input
list. 

We now consider a more efficient function to sort the elements of a
list: *merge sort*. Merge sort works as follows:

-   If the list has fewer than two elements, then it is already sorted.

-   If the list has two or more elements, then we split it into two
    sublists, each with about half the elements, and sort each
    recursively.

-   We merge the two ascending sublists into an ascending list.

For a general implementation, we specify the type of list elements and
the function to be used for the comparison of elements, giving the
following implementation:

~~~{.scala}
    def msort[A](less: (A, A) => Boolean)(ls: List[A]): List [A] =
	{
        def merge(as: List[A], bs: List[A]): List[A] = 
		    (as,bs) match {
                case (Nil,_)                 => bs
		        case (_,Nil)                 => as
                case (Cons(x,xs),Cons(y,ys)) =>
			        if (less(x,y))
			            Cons(x,merge(xs,bs))
			        else
                        Cons(y,merge(as,ys))
        }

        val n = length(ls)/2
        if (n == 0)
		    ls
		else
		    merge(msort(less)(take(ls,n)),
			      msort(less)(drop(ls,n)))
    }
~~~

The `merge`{.scala} forms a tuple of the two lists and does pattern
matching against that tuple. This allowed the pattern match to be
expressed more symmetrically.

The above function uses a function we have not yet defined. 

~~~{.scala}
    def take[A](ls: List[A], n: Int): List[A]
~~~

returns the first `n`{.scala} elements of the list; it is the dual of
`drop`{.scala}.

By nesting the definition of `merge`{.scala}, we enabled it to
directly access the the parameters of `msort`{.scala}.  In particular,
we did not need to pass the comparison function to `merge`{.scala}.

The average case time complexity of `msort`{.scala} is O($n\;
\log(n)$), where $n$ is the length of the input list.

-   Each call level requires splitting of the list in half and merging
    of the two sorted lists.  This takes time proportional to the
    length of the list argument.

-   Each call of `msort`{.scala} for lists longer than one results in
    two recursive calls of `msort`{.scala}.

-   But each successive call of `msort`{.scala} halves the number of
    elements in its input, so there are O($\log(n)$) recursive calls.

So the total cost is O($n\; \log(n)$).  The cost is independent of
distribution of elements in the original list.

We can apply `msort`{.scala} as follows:

~~~{.scala}
    msort((x: Int, y: Int) => x < y)(List(5, 7, 1, 3))
~~~

We defined `msort`{.scala} in curried form with the comparison
function first (unlike what we did with `isort1`{.scala}).  This
enables us to conveniently specialize `msort`{.scala} with a specific
comparison function. For example,

~~~{.scala}
    val intSort     = msort((x: Int, y: Int) => x < y) _
    val descendSort = msort((x: Int, y: Int) => x > y) _
~~~

However, we do have to give explicit type annotations for the
parameters of the comparison function.


## Lists in the Scala standard library

In this discussion (and in Chapter 3 of *Functional Programming in
Scala* \[Chuisano 2015\]), we developed several functions for a simple
`List`{.scala} module.  Our module is related to the builtin Scala
`List`{.scala} module (from `scala.collection.immutable`{.scala}), but
it differs in several ways.

Our `List`{.scala} module is standalone module; the Scala
`List`{.scala} inherits from an abstract class with several traits
mixed in.  These classes and traits structure the interfaces shared
among several data structures in the Scala library. Many of the
functions work for different data structures. For example, in Scala
release 2.12.8 `List`{.scala} is defined as follows:

~~~{.scala}
    sealed abstract class List[+A] extends AbstractSeq[A]
        with LinearSeq[A]
        with Product
        with GenericTraversableTemplate[A, List]
        with LinearSeqOptimized[A, List[A]]
        with scala.Serializable 
~~~

Our `List`{.scala} module consists of functions in which all arguments
must be given explicitly; the Scala `List`{.scala} consists of methods
on the `List`{.scala} class. Scala enables methods with one implicit
argument (i.e. `this`) and one explicit argument to be called as infix
operators with different associativities. It allows symbols such as
`<`{.scala} to be used for method names.

Scala's approach to functional programming uses *method chaining* in
its object system to support composition of pure functions.  Each
method returns an immutable object that becomes the receiver of the
subsequent method call in the same statement.

Extensive use of method chaining in an object-oriented program with
mutable objects---sometimes called a *train wreck*---can make programs
difficult to understand. However, disciplined use of method chaining
helps make the functional and object-oriented aspects of Scala work
together. (In different ways, method chaining is also useful in
development of fluent library interfaces for domain-specific
languages.)

Our `Cons(x,xs)`{.scala} is written as `x :: xs`{.scala} using the
standard Scala library. The `::`{.scala} is a method that has one
implicit argument (the tail list) and one explicit argument (the head
element).

Any Scala method name that ends with a `:`{.scala} is right
associative.  Thus method `x :: xs`{.scala} represents the method call
`xs.::(x)`{.scala}, which in turn calls the data constructor. We can
write `x :: y :: z :: zs`{.scala} without parentheses to mean 
`x :: (y :: (z :: zs))`{.scala}.

We can also use multiple `::`{.scala} constructors in cases for pattern
matching. For example, where we wrote the pattern

~~~{.scala}
    case Cons(x, Cons(y,ys))
~~~

in the `remdups`{.scala} function, we can write the pattern:

~~~{.scala}
    case x :: y :: ys
~~~

Our `append`{.scala} function is normally written with the infix
operator `++`{.scala} in the Scala library. (But there are several
variations for special circumstances.)

Several of our functions with a single list parameter may appear as
parameterless methods with the same name in the Scala library. These
include `sum`{.scala}, `product`{.scala}, `reverse`{.scala}, and
`length`{.scala}. There is also a `head`{.scala} function to retrieve
the head element of a nonempty list.

Our `concat`{.scala} function is parameterless method
`flatten`{.scala} in the Scala library.

Our functions with two parameters, a list and a modifier, are
one-parameter methods with the same name in the Scala library, and,
hence, usable as infix operators.  These include `drop`{.scala},
`dropWhile`{.scala}, `map`{.scala}, `filter`{.scala}, and
`flatMap`{.scala}.  There are also analogous functions `take`{.scala}
and `takeWhile`{.scala}.

Our functions `foldRight`{.scala} and `foldLeft`{.scala}, which have
three parameters, are methods in the Scala library with two curried
parameters. The list argument becomes implicit; the other arguments
are in the same order. The Scala library contains several folding and
reducing functions with related functionality.

Other than `head`{.scala}, `take`{.scala}, `takeWhile`{.scala}, and
the appending and folding methods mentioned above, the Scala List
library has other useful methods such as `forall`{.scala},
`exists`{.scala}, `scanLeft`{.scala}, `scanRight`{.scala},
`zip`{.scala}, and `zipWith`{.scala}.

Check out the Scala API documentation on the Scala website.


## Source Code for Chapter

-   Chapter 3 source: [`List2.scala` ](<List2.scala>) 

-   Chapter 3 tests:  [`TestList2.scala` ](<TestList2.scala>) 


## Exercise Set A

TODO: Edit and reorder these exercises. This order corresponds to
Assignment \#2 in Spring 2019.

In the following exercises, extend the [`List2.scala`](<List2.scala>)
algebraic data type implementation developed in these notes to add the
following functions. In the descriptions below, type `List`{.scala}
refers to the trait defined in that package, not the standard Scala
list.


1.  Write a Scala function `orList`{.scala} that takes a
    `List`{.scala} of `Boolean`{.scala} values and returns the logical
    `or` of the values (i.e. true if any are true, otherwise false).


#.  Write a Scala function `andList`{.scala} that takes a
    `List`{.scala} of `Boolean`{.scala} values and returns the logical
    `and` of the values (i.e. true if all are true, otherwise false).


#.  Write a Scala function `maxList`{.scala} that takes a nonempty
    `List`{.scala} of values and returns its maximum value.
	
	Hint: First solve this with `Int`{.scala}, then generalize to a
    generic type. Study the subsection on insertion sort in this set
    of notes.


#.  Write a Scala `remdups1`{.scala} that is like `remdups`{.scala}
    except that it is implemented using either `foldRight`{.scala} or
    `foldLeft`{.scala}.


#.  Write a Scala function `total`{.scala} that takes a nonnegative
    integer `n`{.scala} and a function `f`{.scala} of an appropriate
    type and returns `f(0) + f(1) + ... f(n)`{.scala}.


#.  Write a Scala function `flip`{.scala} that takes a function of
    polymorphic type `(A,B) => C`{.scala} and returns a function of
    type `(B,A) => C`{.scala} such that, for all `x`{.scala} and `y`{.scala}:

	~~~{.scala}
	    f(x,y) == flip(f)(y,x)
	~~~


#.  Write the following Scala functions using tail recursive definitions:

    a.  `sumT`{.scala} with same functionality as `sum`{.scala}
	#.  `productT`{.scala} with the same functionality as
	    `product`{.scala}


#.  Write a Scala function `mean`{.haskell} that takes a nonempty
    `List`{.scala} of `Double`{.scala} values and returns its mean
    (i.e. average) value.


#.  Write a Scala function `adjPairs`{.scala} that takes a
    `List`{.scala} of pairs (i.e. two-tuples) and returns the
	list of all pairs of adjacent elements. For example,
    `adjPairs(List(2,1,11,4))`{.scala} returns
    `List((2,1),(1,11),(11,4))`{.scala}.

#.  Write a Scala function `splitAt`{.scala} that takes a
    `List`{.scala} of values and an integer `n`{.scala} and returns a
    pair (i.e. two tuple) of `List`{.scala}s, where the first
    component consists of the first `n`{.scala} elements of the input
    list (in order) and the second component consists of the remaining
    elements (in order).


#.  Number base conversion.

    a.  Write a Scala function `natToBin`{.scala} that takes a natural number
        and returns its binary representation as a `List`{.scala} of
        `0`{.scala}’s and `1`{.scala}'s with the most significant digit at the
        head. For example, `natToBin(23)`{.scala} returns
        `List(1,0,1,1,1)`{.scala}. 
		
		In computer science, we usually consider 0 as natural number
        along with the positive integers.

    #.  Generalize `natToBin`{.scala} to Scala function `natToBase`{.scala} 
	    that takes base `b`{.scala} (`b >= 2`{.scala}) as its first
	    paramenter and the natural number as its second. The function
	    should return the base `b`{.scala} representation of the natural
	    number as a list of integer digits with the most significant
	    digit at the head. For example, `natToBase(5,42)`{.scala} returns 
		`List(1,3,2)`{.scala}. 

    #.  Write Scala function `baseToNat`{.scala} that is the inverse of
        the `natToBase`{.scala} function. For any base `b`{.scala}
		(`b >= 2`{.scala}) and natural number `n`{.scala}:

        ~~~{.scala}
            baseToNat(b,natToBase(b,n)) == n
        ~~~


#.  For each of the following specifications, write a Scala function
    that has the given arguments and result.  Use the higher functions
    from the `List`{.scala} algebraic data type from these notes, such
    as `map`{.scala}, `filter`{.scala}, `foldRight`{.scala}, and
    `foldLeft`{.scala}, as appropriate.

    a.  Function `numof`{.scala} takes a value and a list and
        returns the number of occurrences of the value in the list.

    #.  Function `ellen`{.scala} takes a list of lists
        and returns a list of the lengths of the corresponding
        lists.

    #.  Function `ssp`{.scala} takes a list of integers and returns
        the sum of the squares of the positive elements of the list.


		
#.  Write a Scala function `scalarProd`{.scala} with type

    ~~~{.scala}
	    (List[Int],List[Int]):: Int
	~~~

    to compute the scalar product of two lists of integers
    (e.g. representing vectors). 
	
	The *scalar product* is the sum of the products of the elements in
    corresponding positions in the lists.  That is, the scalar product
    of two lists `xs`{.scala} and `ys`{.scala}, of length `n`,
    is:
	
	>   $\sum\limits_{i=0}^{i=n}xs_i * ys_{i}$
	
	For example, `scalarprod(List(1,2,3),List(3,3,3))`{.scala} 
	yields `18`{.scala}.


#.  Write a Scala function `mapper`{.scala} that takes a list of
    functions and a list of values and returns the list of results of
    applying each function in the first list to the corresponding
    value in the second list.


#.  Write a Scala function `removeFirst`{.scala} that takes a
    predicate (i.e. Boolean function) and a list of values and returns
    the list with the first element that satisfies the predicate removed.


#.  Define a Scala function `removeLast`{.scala} that takes a
    predicate (i.e. Boolean function) and a list of values and returns
    the list with the last element that satisfies the predicate removed.

	How	could you define it using `removeFirst`{.scala}?


## General Tree Algebraic Data Type

A *general tree* is a hierarchical data structure in which each node
of the tree has zero or more subtrees. We can define this as a Scala
algebraic data type as follows:

~~~{.scala}
    sealed trait GTree[+A]
    case class Leaf[+A](value: A) extends GTree[A]
    case class Gnode[+A](gnode: List[GTree[A]]) extends GTree[A]
~~~

An object of class `Leaf(x)`{.scala} represents a *leaf* of the tree
holding some value `x`{.scala} of generic type `A`{.scala}. A leaf
does not have any subtrees. It has height 1.

An object of type `Gnode`{.scala} represents an *internal*
(i.e. non-leaf) node of the tree. It consists of a nonempty list of
subtrees, ordered left-to-right. A `Gnode`{.scala} has a height that
is one more than the maximum height of its subtrees.


## Exercise Set B

In the following exercises, write the Scala functions to operate on
the `GTree`{.scala}s. You may use functions from the extended
`List`{.scala} module as needed.

1.  Write Scala function `numLeavesGT`{.scala} that takes a
    `GTree`{.scala} and returns the count of its leaves.
	
#.  Write Scala function `heightGT`{.scala} that takes a
    `GTree`{.scala} and returns its height (i.e. the number of
    levels).

#.  Write Scala function `sumGT`{.scala} that takes a `GTree`{.scala}
    of integers and returns the sum of the values.

#.  Write Scala function `findGT`{.scala} that takes a `GTree`{.scala}
    and a value and returns `true`{.scala} if and only if the element
    appears in some leaf in the tree.

#.  Write Scala function `mapGT`{.scala} that takes a `GTree`{.scala}
    and a function and returns a `GTree`{.scala} with the structure
	but with the function applied to all the values in the tree.

#.  Write Scala function `flattenGT`{.scala} that takes a
    `GTree`{.scala} and returns a `List`{.scala} with the values from
    the tree leaves ordered according to a left-to-right traversal of
    the tree.


## Acknowledgements 

In Spring 2016, I wrote this set of notes to accompany my lectures on
Chapter 3 of the book *Functional Programming in Scala* \[Chiusano
2015\] (i.e. the Red Book). I constructed the notes around the ideas,
general structure, and Scala examples from that chapter and its
associated materials. I also adapted some text and examples from my
*Notes on Functional Programming with Haskell* \[Cunningham 2014\].

I expanded the discussion of algebraic data types, polymorphism, and
variance. For this expansion, I examined other materials including the
Wikipedia articles on Algebraic Data Type, Abstract Data Type,
Polymorphism, Ad Hoc Polymorphism, Parametric Polymorphism, Subtyping,
Liskov Substitution Principle, Function Overloading, and Covariance
and Contravariance \[Wikipedia 2019\]. I also examined the discussion
of variance in the textbook *Programming Scala* \[Wampler 2014\]. I
adapted the sorting algorithms from *Scala by Example* \[Odersky
2014\].

In 2018 and 2019, I updated the format to be more compatible with
evolving document structures. 

In Spring 2019, I also moved the discussion of the kinds of
polymorphism to the new notes on *Type System Concepts*, expanded the
discussion of Variance, and added two exercise sets. Several items
from Exercise Set A were adapted from the list processing chapters of
ELIFP \[Cunningham 2018\].

I maintain these notes as text in Pandoc's dialect of Markdown 
using embedded LaTeX markup for the mathematical formulas and then 
translate the notes to HTML, PDF, and other forms as needed. 


## References

\[Brady 2017\]:
:   Edwin Bradley. *Type-Driven Development with Idris*,
    Manning, 2017.

\[Chiusano 2015\]: 
:   Paul Chiusano and Runar Bjarnason.  *Functional Programming in 
    Scala*, Manning, 2015.  This book is sometimes called the Red 
    Book.  The chapter notes for this book are available on GitHub 
    at <https://github.com/fpinscala/fpinscala/wiki>. 

\[Cunningham 2014\]: 
:   H. Conrad Cunningham. 
    [*Notes on Functional Programming with Haskell* 
    ](<https://john.cs.olemiss.edu/~hcc/csci450/notes/haskell\_notes.pdf>),
    1993-2014.

\[Cunningham 2018\]:
:   H. Conrad Cunningham.  *Exploring Languages with Interpreters and
    Functional Programming*, 2018. Available at 
    <https://john.cs.olemiss.edu/~hcc/csci450/ELIFP/ExploringLanguages.html>.

\[Cunningham 2019a\]: 
:   H. Conrad Cunningham. 
    [*Notes on Scala for Java Programmers*
	](<../ScalaForJava/ScalaForJava.html>), 2019. 

\[Cunningham 2019b\]: 
:   H. Conrad Cunningham. 
    [*Recursion Styles, Correctness, and Efficiency (Scala Version)*
	](<../RecursionStyles/RecursionStylesScala.html>), 2019. 

\[Cunningham 2019c\]: 
:   H. Conrad Cunningham. 
    [*Type System Concepts*
	](<../TypeConcepts/TypeSystemConcepts.html>), 2019. 

\[Odersky 2014\]: 
:   Martin Odersky. *Scala by Example*, EPFL, 2014.

\[Odersky 2016\]: 
:   Martin Odersky, Lex Spoon, and Bill Venners.
    *Programming in Scala*, 3rd Edition, Artima Inc, 2016;
	1st Edition, 2007; 2nd Edition, 2010.

\[Wampler 2014\]:
:   Dean Wampler and Alex Payne.  *Programming Scala*, Second Edition,
    O'Reilly, 2014.

\[Liskov 1987\]:
:   Barbara Liskov. 
    Keynote Address---Data Abstraction and Hierarchy, 
	In the Addendum to the *Proceedings on Object-Oriented Programming
    Systems, Languages, and Applications (OOPSLA '87)*, 
	Leigh Power and Zvi Weiss, Editors, ACM, 1987.
    \[[local 
	](<../../localcopy/Liskov\_Data\_Abstraction\_and\_Hierarchy\_1987.pdf>)\]

\[Petricek 2012\]:
:   Tomas Petricek. *Why Type-first Development Matters*, blog entry,
    August 2012, Accessed 4 March 2019 at
    <http://tomasp.net/blog/type-first-development.aspx/>.
	
\[Wikipedia 2019\]:
:   *Wikipedia*. Articles on Algebraic Data Type, Abstract Data Type,
    Polymorphism (computer science), Ad Hoc Polymorphism, Parametric
    Polymorphism, Subtyping, Liskov Substitution Principle, Function
    Overloading, and Covariance and Contravariance (computer science);
    last accessed 15 February 2019.


## Terms and Concepts

TODO: Update

Function, pure function, referential transparency, side effects,
mutable, immutable, list data type (head, tail, empty), algebraic data
type (composite, sum, product, enumerated), abstract data type, ADT,
syntax, semantics, `trait`{.scala}, `sealed trait`{.scala}, 
`case class`{.scala}, `case object`{.scala}, singleton object,
polymorphism, subtyping, parametric polymorphism, generics,
overloading, type classes, variance (covariant, contravariant,
invariant/nonvariant), following types to implementations (type-driven
or type-first development).
