List Basics and Concatenation

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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?

  • 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]?

  • Division
  • Subtraction
  • Reversal
  • Concatenation (correct)

If L, M, and N are lists, which property must hold true for concatenation (+)?

<p>L + (M + N) = (L + M) + N (Associative) (B)</p> Signup and view all the answers

Which statement correctly describes the 'empty list' ([]) in the context of list concatenation?

<p>It serves as an identity element. (B)</p> Signup and view all the answers

Given L, M, and N as lists, if L + M = L + N, what condition must be met, assuming concatenation is cancellative?

<p>M must equal N. (A)</p> Signup and view all the answers

What does 'x : L' signify in the adapted Haskell notation for lists?

<p>Prepending x to the front of list L. (C)</p> Signup and view all the answers

In 'x : L', what must 'L' be for the expression to be valid?

<p>A list. (A)</p> Signup and view all the answers

What is the head and tail of a non-empty list analogous to in natural numbers?

<p>A number and its successor. (B)</p> Signup and view all the answers

What does Postulate 4 state about the empty list?

<p>It has no head or tail. (B)</p> Signup and view all the answers

According to Postulate 5, if x : L = y : M, which of the following must be true?

<p>x = y and L = M (B)</p> Signup and view all the answers

What does the Axiom of List Induction ensure about lists?

<p>All lists are finite. (B)</p> Signup and view all the answers

What are you required to prove in the 'basis' step of list induction?

<p>That the property holds for the empty list. (B)</p> Signup and view all the answers

What is the main goal of 'List Induction'?

<p>To prove some property is true for all lists. (B)</p> Signup and view all the answers

According to Algorithm 7, what is the length of the empty list []?

<p>0 (C)</p> Signup and view all the answers

What does len(x: K) = len(K)~ mean in the context of Algorithm 7?

<p>The length of x: K is the same as incrementing the length of K. (A)</p> Signup and view all the answers

According to Algorithm 8, how is the concatenation of two lists, L and M, denoted?

<p>L + M (D)</p> Signup and view all the answers

According to Algorithm 8, what is [] + M?

<p>M (D)</p> Signup and view all the answers

Using Algorithm 8, what is the result of (x: K) + M?

<p>x : (K + M) (D)</p> Signup and view all the answers

What is a key idea behind defining operations on lists recursively?

<p>Specifying how the operation affects the empty list and lists with a head and tail. (B)</p> Signup and view all the answers

Given Proposition 18, what is the identity element for concatenation?

<p>[] (D)</p> Signup and view all the answers

For lists L, M, and N, which expression demonstrates the associative property of concatenation?

<p>L + (M + N) = (L + M) + N (C)</p> Signup and view all the answers

If L + M = L + N, what does it imply, assuming concatenation is cancellative?

<p>M = N (C)</p> Signup and view all the answers

What useful fact relates the length of concatenated lists, len(L + M), to the lengths of the individual lists?

<p>len(L + M) = len(L) + len(M) (D)</p> Signup and view all the answers

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?

<p>Iteratively append each element of M to L. (D)</p> Signup and view all the answers

If L = [a, b, c, d, e], and list indices start from 0, what does L₃ represent?

<p>d (A)</p> Signup and view all the answers

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?

<p>L[1:4] (A)</p> Signup and view all the answers

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?

<p>No. (D)</p> Signup and view all the answers

In the context of list sorting, what properties must an operation, ⊆, have to be considered a comparison?

<p>It must be reflexive and transitive. (A)</p> Signup and view all the answers

Flashcards

What is a list?

An ordered collection of items, where order matters.

What is concatenation?

Combining two lists into a single list.

List concatenation is associative.

For lists L, M, and N: L + (M + N) = (L + M) + N.

What is an empty list?

A list with no elements, denoted by [].

Signup and view all the flashcards

Empty list as identity

For any list L: [] + L = L = L + [].

Signup and view all the flashcards

List Concatenation Cancellative.

If L + M = L + N, then M = N. If L + N = M + N, then L = M.

Signup and view all the flashcards

What is Prepending?

Building a list by adding items to the front. Indicated by x : L.

Signup and view all the flashcards

What is the head of a list?

The first element in a list.

Signup and view all the flashcards

What is the tail of a list?

The remainder of the list after removing the head.

Signup and view all the flashcards

Empty list has no head/tail.

For any thing x and any list L, [] ≠ x : L.

Signup and view all the flashcards

Uniqueness of head and tail

If x : L = y : M, then x = y and L = M.

Signup and view all the flashcards

List Induction Axiom

Ensures that all lists are finite.

Signup and view all the flashcards

List Induction Method

Used to prove a property is true for all lists using a basis, inductive hypothesis, and inductive step.

Signup and view all the flashcards

What is the length of a list?

The number of elements in the list.

Signup and view all the flashcards

Algorithm for Length of a List

Given lists L and M, the length and the concatenation of L and M.

Signup and view all the flashcards

What are Homomorphisms?

A function that translates one operation into another: len translates + into +.

Signup and view all the flashcards

What is a Monoid?

A clearly defined set equipped with an associative operation and an identity for the operation.

Signup and view all the flashcards

What are List Monoids?

A monoid consisting of lists with items drawn from a set X, with concatenation as the operation and the empty list as the identity.

Signup and view all the flashcards

Monoid Homomorphisms

A function h: X -> Y satisfying h(e) = 1 and h(x * y) = h(x) * h(y).

Signup and view all the flashcards

List Extended to Operations

Applies a function f to each item on a list by recursion.

Signup and view all the flashcards

List Indexing

A method to map an item of a list given its position in the list.

Signup and view all the flashcards

Slicing a List

The sublist consisting of items indexed i, i+1, ..., j-1.

Signup and view all the flashcards

Prefix Ordering of Lists

L is a prefix of M if L + D = M for some list D.

Signup and view all the flashcards

Sorted Lists

Elements appear in increasing order via some partialorder.

Signup and view all the flashcards

Eagerness

An eager structure is a data structure which can give you the entire contents of the structure, even if that is much more than you requested .

Signup and view all the flashcards

Laziness

A lazy structure, on the other hand, will respond by evaluating only as much of its contents as needed to satisfy the request.

Signup and view all the flashcards

Streams

Infinite lists.

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, then M = N. Likewise, if L + N = M + N then L = M
  • If [x] = [y], then x = 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 prepend e to get [e], then b to get [b, e], then a 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 with x as the head and L as the tail
  • Square brackets can be used as abbreviations
  • colon (:) associates from right to left
  • a : b makes sense only when b 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, then x = y and L = 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, then L 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 list x : 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 and M:
  • [] + 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 and L + [] = 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 and N, L + (M + N) = (L + M) + N

Concatenation is Cancellative

  • For lists L, M and N:
  • If L + M = L + N, then M = N
  • If L + N = M + N, then L = M

Length and Concatenation

  • Length is a homomorphism with respect to concatenation and addition with the following properties:
  • len([]) = 0
  • For any lists L and M, 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 with x 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 from 0
  • 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 no i < len([])
  • Li is defined as follows:
  • []i is never defined because i < len([])
  • (x : L)0 = x
  • (x : L)k+1 = Lk provided that Lk is defined

List Slicing

  • Extract the sublist of L consisting of items indexed i, i + 1, ... j – 1, writing L[i : j] for the list [ai, ai+1,... aj−1].

List Slicing Algorithm:

  • For a list L and natural numbers i ≤ 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, if L + 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 and L ∈ List[X], then x : 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 and Y are sets, and f: X → Y is an operation that transforms elements of X into elements of Y
  • Then define an operation from List[X] to List[Y] that applies f 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.

Quiz Team

Related Documents

More Like This

List Interface in Java
5 questions

List Interface in Java

WieldyPhiladelphia avatar
WieldyPhiladelphia
List of -ologists Flashcards
15 questions
Use Quizgecko on...
Browser
Browser