row of penguins

Grouping values with lists and structs, and transforming them with map in racket/scheme

The video tutorials below are to help you learn how to use aggregates such as lists and structs, and to map functions over lists. Of course you should get DrRacket for your computer platform, to work through the examples.

In case videos aren't your preferred source for learning, I've also included some other resources with each video.

lists of values video

Group values into lists and then transform the list — extract its first element, produce the reverse list, produce the rest of the list (all but the first element). After you're done with the video you should be able to:

  • Reverse the list (list 1 3 5 7 9 11).
  • Produce the middle element of a 5-element list
  • Use range to produce the list of integers from 1 to 20

Some non-video resources:

  • Picturing Programs Chapter 22 discusses lists.
  • DrRackets Intermediate Student with lambda list functions.

structs of values video

Group values as named parts of structures (or structs). Define a new structure, create a few instances of that structure, and then access the named parts. After you're done with the video, you should be able to:

  • Define a new structure called book with parts title, author, and length.
  • Create a few instances of your new structure book, and then extract the parts.

Some non-video resources:

  • Picturing Programs Chapter 20, and Chapter 21 discuss structs.
  • DrRacket's Intermediate Student with Lambda make-struct and related functions (use Control-F to search the page).

map lists into new lists video

Create a new list by transforming each list element using the same function. This extremely powerful idea captures a great deal of what programmers need to do. After you're done with the video, you should be able to:

  • Use map and the function / to transform the list (list 1 2 3 4) into the list of reciprocals (list 1 1/2 1/3 1/4).
  • Use map and the function flip-horizontal to transform a list of images into another list of those images flipped horizontally.
  • Use map and a function that you create called sub5 to transform the list (list 1 2 3 4 5) into the list (list -4 -3 -2 -1 0).

Some non-video resources that may be useful:

  • Guide to DrRacket's Intermediate Student with lambda map function.

decompose and reconstruct images video

Deconstruct images into lists of color structs together with information such as width and height. Once you take images apart, you can get at the colors, and completely transform them. Once you're done with the video, you should be able to:

  • Display the 7th color from an arbitrary image.
  • Repackage an image with a different width and height, provided they multiply to the same product.
  • Create a function called cycle-colors that creates a new color from an old one by putting the old blue in the red position, the old red in the green position, and the old green in the blue position. The alpha should stay as it was. Once you've done this, you can use map-image to transform an image into its color-cycled version.
  • Create a function called quarter-green that produces a new color from an old one by reducing the green component to a quarter (use quotient). Once you've done this, you can use map-image to transform an image into its quarter-blue version.

Some non-video resources you may find useful:

  • Section 20.7 of Picturing Programs describes how to use the color struct (just the red, green, and blue parts), and later Section 20.7.3 introduces map-image. Note that you are allowed to use it with a function that consumes a color and produces a color (it doesn't need x or y information).
  • Guide to DrRacket's h2dp/images image->color-list and color-list->bitmap

apply functions to lists video

Generate powerful effects by taking an arbitrary function and then applying it to a sequence of values in a list. Once you've watched the video, try the exercise below:

	      > (require picturing-programs)
	      > (define L1 (list 1 2 3 4 5))
	      > (define L2 (list "how" "now" "brown" "cow"))
	      > (define L3 (list
                            (square 5 "solid" "green")
                            (square 6 "solid" "blue")
                            (square 7 "solid" "red)))
	      > (define L4 (list 2 (square 5 "solid" "green")))
	      > L1
	      > (apply + L1)
	      > (apply * L1)
	      > L3
	      > (apply above L3)
	      > L2
	      > (apply string-append L2)
	      > L4
	      >(apply scale L4)
	    
Think about what you've seen above until you can predict, before you type them into DrRacket, what is produced by the examples below:
	      > (apply + (list 2 3 4 5 6))
	      > (apply beside (list pic:hacker pic:calendar))
	      > (apply string-append (list "three" "blind" "mice"))   
	    

filter unwanted elements from lists video

Customize lists by generating a new list consisting of only the elements you choose.


Danny Heap
Last modified: Sat Nov 9 20:59:17 EST 2013