Lists

List Basics Overview:

In Scheme (and Scamper), a list is a fundamental data structure that holds collections of values. Lists are essential for handling multiple data points at once, making them especially useful in more complex problems, such as those found in data science.

The document introduces the basic concepts of lists in Scheme, how to create lists, and the operations that can be performed on them.


Five Basic Concepts for a New Data Type:

  1. Name: The data type is called a list.
  2. Purpose: The main purpose of a list is to group or collect values.
  3. How to Express Values: You can create and manipulate lists using specific functions.
  4. Displaying Values: Lists are displayed either as function calls (in Scamper) or as parenthesized lists in Scheme.
  5. Operations Available: A variety of functions can be used to manipulate and build lists.

Displaying Lists:


Creating Lists:


Common List Operations:

  1. length: Returns the number of elements in the list:

    (length (list 3 4 5)) ; produces 3
    
  2. list-ref: Accesses an element of the list at a given index (0-based):

    (list-ref (list 1 2 3 4) 2) ; produces 3
    
  3. index-of: Returns the position of the first occurrence of a value in a list:

    (index-of (list "a" "b" "c") "b") ; produces 1
    
  4. reverse: Reverses the list:

    (reverse (list 1 2 3 4 5)) ; produces (5 4 3 2 1)
    
  5. append: Combines two lists into one:

    (append (list 1 2) (list 3 4)) ; produces (1 2 3 4)
    
  6. list-take and list-drop:

    • list-take: Takes the first n elements from a list.
      (list-take (list 1 2 3 4 5) 3) ; produces (1 2 3)
      
    • list-drop: Drops the first n elements from a list.
      (list-drop (list 1 2 3 4 5) 2) ; produces (3 4 5)
      

Lists in Scheme provide a versatile way to group and manage data. You can create lists with a wide variety of functions, manipulate them with operations like append, reverse, list-ref, and more, and use them to handle sequences of data efficiently.

List Transformations Overview:

In computing, list transformations involve transforming individual elements of a collection, which can be efficiently done using higher-order functions like map and apply. This document explains how transformations over collections (lists) are central to functional programming and how they can be performed in Scheme.


Transformation through map:

The map function is a higher-order function in Scheme that transforms each element of a list by applying a given function to every element. It takes two arguments:

  1. A function that transforms a single element.
  2. A list of elements to transform.

For example, if you have a function that adjusts salaries based on a cost-of-living adjustment (COLA):

(define compute-cola-salary
  (lambda (salary)
    (+ salary (* salary 0.016))))

You can use map to apply this function to a list of salaries:

(map compute-cola-salary (list 100000 100000 50000 75000 500000))

This produces:

(list 101600 101600 50800 76200 508000)

Transformation Flexibility:

Transforming Other Types of Data:

map can transform any collection, not just numbers. For example:

Errors and Type Matching:


Using apply:

While map applies a function to each element of a list, apply takes a procedure and applies it to an entire list of arguments at once.

Dealing with Complex Data:

Sometimes transformations involve more complex collections like images. Consider transforming a list of numbers into a series of green circles:

(define green-circle
  (lambda (radius)
    (circle radius "solid" "green")))
(map green-circle (list 20 40 60 40 20))

This produces a list of circles with different radii, which you can then combine using functions like beside or above.

Error Handling in map and apply:

Summary:

List transformations are a powerful tool in functional programming. The map function is central to transforming individual elements of a list based on a defined function. Additionally, apply allows you to apply a function to an entire list at once. These transformations are crucial for efficiently manipulating collections of data.