Podcast
Questions and Answers
In the context of lists, what distinguishes [1, 2, 3] from [3, 2, 1]?
In the context of lists, what distinguishes [1, 2, 3] from [3, 2, 1]?
- The presence of brackets.
- The total number of elements.
- The type of elements.
- The order of elements. (correct)
Why might dropping brackets and commas from a list of digits, such as writing 201 instead of [1, 0, 2], require careful consideration?
Why might dropping brackets and commas from a list of digits, such as writing 201 instead of [1, 0, 2], require careful consideration?
- It is not a valid list representation.
- It necessitates a clear understanding of the intended order. (correct)
- It always makes the list easier to read.
- It maintains clarity even with varying digit orders.
What operation is naturally used to combine the lists [4, 5, 1] and [3, 0, 9] to produce [4, 5, 1, 3, 0, 9]?
What operation is naturally used to combine the lists [4, 5, 1] and [3, 0, 9] to produce [4, 5, 1, 3, 0, 9]?
- Division
- Subtraction
- Reversal
- Concatenation (correct)
If L, M, and N are lists, which property must hold true for concatenation (+)?
If L, M, and N are lists, which property must hold true for concatenation (+)?
Which statement correctly describes the 'empty list' ([]) in the context of list concatenation?
Which statement correctly describes the 'empty list' ([]) in the context of list concatenation?
Given L, M, and N as lists, if L + M = L + N, what condition must be met, assuming concatenation is cancellative?
Given L, M, and N as lists, if L + M = L + N, what condition must be met, assuming concatenation is cancellative?
What does 'x : L' signify in the adapted Haskell notation for lists?
What does 'x : L' signify in the adapted Haskell notation for lists?
In 'x : L', what must 'L' be for the expression to be valid?
In 'x : L', what must 'L' be for the expression to be valid?
What is the head and tail of a non-empty list analogous to in natural numbers?
What is the head and tail of a non-empty list analogous to in natural numbers?
What does Postulate 4 state about the empty list?
What does Postulate 4 state about the empty list?
According to Postulate 5, if x : L = y : M, which of the following must be true?
According to Postulate 5, if x : L = y : M, which of the following must be true?
What does the Axiom of List Induction ensure about lists?
What does the Axiom of List Induction ensure about lists?
What are you required to prove in the 'basis' step of list induction?
What are you required to prove in the 'basis' step of list induction?
What is the main goal of 'List Induction'?
What is the main goal of 'List Induction'?
According to Algorithm 7, what is the length of the empty list []?
According to Algorithm 7, what is the length of the empty list []?
What does len(x: K) = len(K)
~ mean in the context of Algorithm 7?
What does len(x: K) = len(K)
~ mean in the context of Algorithm 7?
According to Algorithm 8, how is the concatenation of two lists, L and M, denoted?
According to Algorithm 8, how is the concatenation of two lists, L and M, denoted?
According to Algorithm 8, what is [] + M
?
According to Algorithm 8, what is [] + M
?
Using Algorithm 8, what is the result of (x: K) + M?
Using Algorithm 8, what is the result of (x: K) + M?
What is a key idea behind defining operations on lists recursively?
What is a key idea behind defining operations on lists recursively?
Given Proposition 18, what is the identity element for concatenation?
Given Proposition 18, what is the identity element for concatenation?
For lists L, M, and N, which expression demonstrates the associative property of concatenation?
For lists L, M, and N, which expression demonstrates the associative property of concatenation?
If L + M = L + N, what does it imply, assuming concatenation is cancellative?
If L + M = L + N, what does it imply, assuming concatenation is cancellative?
What useful fact relates the length of concatenated lists, len(L + M), to the lengths of the individual lists?
What useful fact relates the length of concatenated lists, len(L + M), to the lengths of the individual lists?
Suppose you have defined the list operation L^x, which appends the element x to the end of the list L. How would you define concatenation using this appending operator?
Suppose you have defined the list operation L^x, which appends the element x to the end of the list L. How would you define concatenation using this appending operator?
If L = [a, b, c, d, e], and list indices start from 0, what does L₃ represent?
If L = [a, b, c, d, e], and list indices start from 0, what does L₃ represent?
If L = [a, b, c, d, e], how would you express the sublist containing elements from index 1 up to (but not including) index 4 using slice notation?
If L = [a, b, c, d, e], how would you express the sublist containing elements from index 1 up to (but not including) index 4 using slice notation?
Suppose L=[1, 2, 3] and M=[4, 5, 6]. According to the definition of prefix ordering of lists, is L a prefix of M?
Suppose L=[1, 2, 3] and M=[4, 5, 6]. According to the definition of prefix ordering of lists, is L a prefix of M?
In the context of list sorting, what properties must an operation, ⊆, have to be considered a comparison?
In the context of list sorting, what properties must an operation, ⊆, have to be considered a comparison?
Flashcards
What is a list?
What is a list?
An ordered collection of items, where order matters.
What is concatenation?
What is concatenation?
Combining two lists into a single list.
List concatenation is associative.
List concatenation is associative.
For lists L, M, and N: L + (M + N) = (L + M) + N.
What is an empty list?
What is an empty list?
Signup and view all the flashcards
Empty list as identity
Empty list as identity
Signup and view all the flashcards
List Concatenation Cancellative.
List Concatenation Cancellative.
Signup and view all the flashcards
What is Prepending?
What is Prepending?
Signup and view all the flashcards
What is the head of a list?
What is the head of a list?
Signup and view all the flashcards
What is the tail of a list?
What is the tail of a list?
Signup and view all the flashcards
Empty list has no head/tail.
Empty list has no head/tail.
Signup and view all the flashcards
Uniqueness of head and tail
Uniqueness of head and tail
Signup and view all the flashcards
List Induction Axiom
List Induction Axiom
Signup and view all the flashcards
List Induction Method
List Induction Method
Signup and view all the flashcards
What is the length of a list?
What is the length of a list?
Signup and view all the flashcards
Algorithm for Length of a List
Algorithm for Length of a List
Signup and view all the flashcards
What are Homomorphisms?
What are Homomorphisms?
Signup and view all the flashcards
What is a Monoid?
What is a Monoid?
Signup and view all the flashcards
What are List Monoids?
What are List Monoids?
Signup and view all the flashcards
Monoid Homomorphisms
Monoid Homomorphisms
Signup and view all the flashcards
List Extended to Operations
List Extended to Operations
Signup and view all the flashcards
List Indexing
List Indexing
Signup and view all the flashcards
Slicing a List
Slicing a List
Signup and view all the flashcards
Prefix Ordering of Lists
Prefix Ordering of Lists
Signup and view all the flashcards
Sorted Lists
Sorted Lists
Signup and view all the flashcards
Eagerness
Eagerness
Signup and view all the flashcards
Laziness
Laziness
Signup and view all the flashcards
Streams
Streams
Signup and view all the flashcards
Study Notes
- Natural numbers are examples of building bigger objects from smaller ones
- Lists are analogous to natural numbers
List Basics
- Lists are familiar, like "to do" items or alphabetized names
- Base ten numerals (e.g., 30295) are lists of digits
- Words, sentences, and paragraphs can also be considered lists
- When thinking carefully about lists, a more formal approach is helpful
- Square brackets are used, common in programming languages, e.g.,
[1, 3, 2, 1]
- Order matters, so
[1, 2, 3, 1]
is different from[1, 3, 2, 1]
- Brackets and commas are not always needed, like writing
201
instead of[1, 0, 2]
- When dropping brackets/commas, it should be clear what the list represents
Concatenation
- Concatenation joins two lists, for example,
[2, 5, 1]
+[3, 0, 9]
=[2, 5, 1, 3, 0, 9]
- Haskell uses
++
, Python uses+
, mathematicians use ⊗ or sometimes · - This text adopts Haskell's notation (+) for concatenation
Properties of Concatenation
- Associativity:
L + (M + N) = (L + M) + N
- Identity:
[] + L = L = L + []
- An empty list exists and serves as an identity - Cancellative: If
L + M = L + N
, thenM = N
. Likewise, ifL + N = M + N
thenL = M
- If
[x] = [y]
, thenx = y
- Lists are finite
Building Lists
- Lists can be built one item at a time, similar to incrementing natural numbers
- Prepending items is a common approach in computer science
- To get
[a, b, e]
, start with[]
, then prepende
to get[e]
, thenb
to get[b, e]
, thena
to get[a, b, e]
- Haskell notation:
x : L
indicates prepending - "Cons" is the historical pronunciation, short for "construct"
Terminology
- Empty list:
[]
- Head: The first element of the list
- Tail: The rest of the list
x : L
represents a list withx
as the head andL
as the tail- Square brackets can be used as abbreviations
- colon (:) associates from right to left
a : b
makes sense only whenb
is already a list
Homogeneous and Heterogeneous Lists
- List may consist of similar items (natural numbers/alphabetical chars)
- Lists can be heterogeneous (a mixed list);
[4, a]
is permitted by the vocabulary
Postulates for Lists
- Empty list has no head or tail:
[] ≠x : L
- Lists that are not empty can only be built one way
- Uniqueness of head and tail: if
x : L = y : M
, thenx = y
andL = M
Axiom of List Induction
- Anything not required by the vocabulary does not exist
- No lists can be eliminated without violating the basic vocabulary
- Every nonempty list has a head and a tail
Proof by Contraposition
- if
L
does not have a head or tail, thenL
is not a list
List Vocabulary
- The vocabulary for lists declares that anything can be an item on a list, including mathematical things
- The collection of mathematical "things" is open-ended
List Induction
- Achieves proofs about lists using a scheme almost identical to simple arithmetic induction
List Induction Steps
- Basis: Prove the property is true for
[]
- Inductive hypothesis: Assume property is true for list
K
- Inductive step: Prove that for any thing
x
, the property is true for the listx : K
- Conclusion: Property is true for all lists
Recursion
- Useful for defining concepts associated with lists
- Every list has a length, which is a natural number
List Length
- Denoted by
len(L)
len([]) = 0
len(x : K) = len(K) + 1
List Concatenation
- Defined as follows for list
L
andM:
[] + M = M
(x : K) + M = x : (K + M)
for any thing x and any list K
Recursion for Lists
- Requires specifying how the operation affects the empty list and how it affects a list with a head and tail, lists of the form
x : M
Proposition:
[]
is the identity for+
[] + L = L
andL + [] = L
- By definition,
[] + L = L
is always true - The second equality is proven by list induction on L
List Concatenation Associativity
- For All lists
L, M
andN
,L + (M + N) = (L + M) + N
Concatenation is Cancellative
- For lists
L, M
andN:
- If
L + M = L + N
, thenM = N
- If
L + N = M + N
, thenL = M
Length and Concatenation
- Length is a homomorphism with respect to concatenation and addition with the following properties:
len([]) = 0
- For any lists
L
andM
,len(L + M) = len(L) + len(M)
List Operation
- Appending an item to the end of a list. That is, define an algorithm for
L ^ x,
so that the result is L withx
appended to the end - For example,
[3, 2, 7] ^ 5
should result it[3, 2, 7, 5]
.
List Reversal
- An operation
rev
on lists so that, for example,rev([2, 3, 4]) is [4, 3, 2]
List Structure
- In a list
L
, items appear in order - Items are referred to via their position in list
- Can start counting either from
1
or from0
- Starting from
0
makes calculations simpler - Formula for indexed items in a List is as follows. Suppose L is a list and i is a natural number so that
i < len(L)
- Note that
len([]) = 0
, so there is noi < len([])
- Li is defined as follows:
[]i
is never defined becausei < len([])
(x : L)0 = x
(x : L)k+1 = Lk
provided thatLk
is defined
List Slicing
- Extract the sublist of
L
consisting of items indexedi, i + 1, ... j – 1
, writingL[i : j]
for the list[ai, ai+1,... aj−1]
.
List Slicing Algorithm:
- For a list
L
and natural numbersi ≤ j ≤ len(L)
,L[i : j]
is a list defined by the following: L[0:0] = []
(x: L) [0: j] = x : (L[0: j])
(x: L)[i: j] = L[i : j]
Prefix Order
- For Lists L and M, say that L is a Prefix of M, writing
L ⊆ M
, ifL + D = M
for some list D
List Monoids
- If X is a set, then (List[X], +, []) is the monoid consisting of lists with items drawn from X
- The set List[X] is defined by
[] ∈ List[X]
- If
x ∈ X
andL ∈ List[X]
, thenx : L∈ List[X]
- Nothing else is in List[X]
Homogeneous Lists
- We will commonly need to consider homogeneous lists — lists in which all items are drawn from the same collection
- When that collection is a clearly defined set, these lists also comprise a clearly defined set
- And since we already know that concatenation is an associative operation with identity
[]
, this set actually forms a monoid
List Homomorphisms
- Homomorphism is a function
h: X → Y
satisfying h(e) = 1
h(x+y) = h(x) * h(y)
List as Functor
- Definition: List extended to operations
- Suppose
X
andY
are sets, andf: X → Y
is an operation that transforms elements ofX
into elements ofY
- Then define an operation from
List[X]
toList[Y]
that appliesf
to each item on a list by recursion List[f]([]) = []
List [f] (x: K) = f(x) : List[f] (K)
Folding
- Can be used to define list homomorphisms
fold([]) = e
fold(x: K) = x *fold(K)
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.