Representing Images as Lists of Spots

Similar documents
Laboratory: Recursion Basics

Repetition Through Recursion

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Introduction to Scheme

Functional Programming

Lisp. Versions of LISP

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Scheme: Expressions & Procedures

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

6.001: Structure and Interpretation of Computer Programs

A Brief Introduction to Scheme (II)

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Functional Programming. Pure Functional Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Modern Programming Languages. Lecture LISP Programming Language An Introduction

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

CSc 520. Principles of Programming Languages 7: Scheme List Processing

ALISP interpreter in Awk

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

1.3. Conditional expressions To express case distinctions like

Data abstraction, revisited

CSC 533: Programming Languages. Spring 2015

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

Project 2: Scheme Interpreter

Functional Programming. Contents

Lecture Notes on Lisp A Brief Introduction

CSc 520 Principles of Programming Languages

Functional Programming. Pure Functional Languages

How to Design Programs Languages

Imperative, OO and Functional Languages A C program is

Intro. Scheme Basics. scm> 5 5. scm>

Text File Databases. Contents. Appendices 7 Source code... 7 Test code Input file readers

Documentation for LISP in BASIC

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

Functional programming in LISP

CS 360 Programming Languages Interpreters

An Explicit-Continuation Metacircular Evaluator

LISP Programming. (23 (this is easy) hello 821)

Scheme Quick Reference

User-defined Functions. Conditional Expressions in Scheme

6.001 Notes: Section 6.1

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Lambda Calculus see notes on Lambda Calculus

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Lisp Basic Example Test Questions

Building a system for symbolic differentiation

4.2 Variations on a Scheme -- Lazy Evaluation

Scheme Quick Reference

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Discussion 11. Streams

Functional Programming. Pure Functional Programming

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))


Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

by the evening of Tuesday, Feb 6

Functional Programming. Big Picture. Design of Programming Languages

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

5Data. 5.1 Types. scalar

Building a system for symbolic differentiation

5Data. 5.1 Types. scalar. datatype

SOFTWARE ARCHITECTURE 6. LISP

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Artificial Intelligence Programming

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CS 457/557: Functional Languages

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

CS 61A Discussion 8: Scheme. March 23, 2017

Lists in Lisp and Scheme

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS 275 Name Final Exam Solutions December 16, 2016

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Essentials of Programming Languages Language

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Essentials of Programming Languages Language

University of Massachusetts Lowell : Organization of Programming Languages Spring 2004 Problem Set 4. Henderson Picture Language

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

1. The Square limit language

Common LISP-Introduction

Typed Racket: Racket with Static Types

CS61A Notes Disc 11: Streams Streaming Along

Discussion 4. Data Abstraction and Sequences

3 ADT Implementation in Java

FP Foundations, Scheme

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

UMBC CMSC 331 Final Exam

Scheme: Strings Scheme: I/O

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Transcription:

Fundamentals of Computer Science I: Media Computing (CS151.02 2007F) Representing Images as Lists of Spots Summary: We examine Scheme s list data type and an application of that type to representing images. Contents: Introduction: Representing Data Scheme s Lists to the Rescue Creating Lists with list Nested Lists Constructing Lists with cons Taking Lists Apart List Predicates Other Common List Procedures length reverse append list-ref Introduction: Representing Data As we ve said a few times, computer scientists study the algorithms we write to manipulate information and the ways in which we represent information. Since an emphasis of this course is images, we have been emphasizing kinds of data related to images. You may not have noticed it, but we have developed a number of ways to represent both colors and images. For the case of colors, we can represent them as names (e.g., "blood red"), as RGB colors (often computed with the rgb.new, as in (rgb.new 102 0 0)), and as strings that list the three components (e.g., "102/0/0"). For the case of images, we can represent them as a grid of rgb color values that you can get or set. However, as we ve discovered with our experiments with region.compute-pixels!, a function from position to color is sufficient to describe many images. It s now time to consider another way to think about images. Particularly for images that have only a few pixels set, we can describe the image by indicating the positions and colors of those pixels. (We can also describe a small part of the image in this way. Such descriptions can then act as a form of stamp for the image.) We call call the combination of position and color a spot. 1

Scheme s Lists to the Rescue Fortunately, Scheme, like Lisp before it, provides a simple and elegant way to represent collections of data, the list. In contrast to Scheme s unstructured data types, such as symbols and numbers, lists are structures that contain other values as elements. In particular, a list is an ordered collection of values. In Scheme, lists can be heterogeneous, in that they may contain different kinds of values. As we will see throughout this semester, lists provide a simple, relatively convenient, and elegant mechanism for organizing collections of information. How does Scheme show you that something is a list? It surrounds the list with parentheses and separates the items with spaces. For example, the following is the list of three color names that we like. ("red" "grey" "black") For our problem of representing simple images, we can create a list of spots, where each spot consists of a column, row, and color. (How do we represent the combined column, row, and color? With another list.) For example, here is the list that might correspond to a white spot at position (10,11) (recall that -1 seems to be the integer that DrFu uses for white). (10 11-1) Similarly, here is a list that might represent red spots at (10,1), (10,2), and (10,3); grey spots at (11,2), (11,3), and (11,4), and black spots at (12,3), (12,4), and (12,5). Note that black seems to be represented by 255, grey by -1465341697, and red by -16776961. ((10 1-16776961) (10 2-16776961) (10 3-16776961) (11 2-1465341697) (11 3-2139062017) (11 4-2139062017) (12 3 255) (12 4 255) (12 5 255)) Of course, to achieve the goal of usefully representing images as lists of spots, we need to know more than what those lists might look like when printed. We must also know basic operations we can use for processing those lists, and others. These include operations for creating lists, for extracting information from lists, for extending lists, and for dealing with each element in a list. Creating Lists with list The easiest way to create a list is to invoke a procedure named list. This procedure takes as many parameters as you care to give it and packs them together into a list. many of them there may be, and packs them into a list. Just as the addition procedure + sums its arguments and returns the result, so the list procedure collects its arguments and returns the resulting list: > (list "red" "grey" "black" "white") ("red" "grey" "black" "white") > (list) > (list 1 2 3 4 5 6 7) (1 2 3 4 5 6 7) > (list "red" (rgb.new 255 0 0) (rgb->string (cname->rgb "red"))) 2

("red" -16776961 "255/0/0") > (list 10 11 (rgb.new 255 255 255)) (10 11-1) > (list 11 2 color.grey) (11 2-1465341697) > (list (list 10 1 color.red) (list 10 2 color.red) (list 10 3 color.red) (list 11 2 color.grey) (list 12 3 color.grey)) ((10 1-16776961) (10 2-16776961) (10 3-16776961) (11 2-1465341697) (11 3-2139062017)) Nested Lists You may recall that we said that a list can contain any types of Scheme values. As the last two examples above suggest, lists can even contain other lists as values. The technique of placing one list inside another is called nesting lists, and is useful in a wide variety of situations. Most typically, we nest lists when the element lists are used for a different purpose than the enclosing lists. For example, in the examples above, the element lists each represent one spot (that is, column, row, and color) and the list of elements represents a collection of spot. We are not limited to this simple nesting. We can nest lists within lists within lists within lists, as deep as we desire to go. Not all of our nested values need to be lists. We can, for example, make lists some of whose elements are lists, some of whose elements are strings, some of whose elements are numbers, and so on and so forth. Constructing Lists with cons Although list is a convenient way to build lists, the list operation is, itself, composed of other, more primitive, operations. Behind the scenes, list invokes cons once for each element of the completed list, to hook that element onto the previously created list. The cons procedure takes two parameters, a value and a list, and builds a new list by prepending the value onto the list. The prepended value is called the car of the resulting list. The previous list (that is, the list that has now had a new value added to the front) is called the cdr of the resulting list. > (cons 5 (list 1 2 3)) (5 1 2 3) Why is it called cons instead of list.prepend or something similar? Well, that s the name John McCarthy, the designer of Lisp, chose about forty years ago. It s short for construct, because cons constructs lists. (The habit of naming procedures with the basic type they operate on, a period, and the key operation did not start until a few decades later.) The names car and cdr were chosen for very specific reasons that will not make sense for a few more weeks. 3

Scheme s name for the empty list is a pair of parentheses with nothing between them:. Most implementations of Scheme suggest permit you to refer to that list as nil or null. A few permit you to type it verbatim. All permit you to describe the empty list by putting a single quote before the pair of parentheses. > > nil > null You will find that we prefer to use a name for that list. If sample code does not work in your version of Lisp, try inserting the following definitions. (define nil ) (define null ) Note that by using cons and nil, we can build up a list of any length by repeatedly prepending a value to the list. > (define singleton (cons "red" null)) > singleton ("red") > (define doubleton (cons "green" singleton)) > doubleton ("green" "red") > (define tripleton (cons "yellow" doubleton)) > tripleton ("yellow" "green" "red") > (cons "senior" (cons "third-year" (cons "second-year" (cons "freshling" null)))) ("senior" "third-year" "second-year" "freshling") You may note that lists built in this way seem a bit backwards. That is, the value we add last appears at the front, rather than at the back. However, that s simply the way cons works and, as the last example suggests, in many cases it is a quite sensible thing to do. Taking Lists Apart So far, so good. We now know how to build lists using two techniques: Using list and using a combination of cons and nil. Of course, creating lists is not very useful unless we can extract the individual elements of the list. The two most basic operations one uses to recover elements from a list are car and cdr. The car procedure takes as a parameter a non-empty list and returns the first element. The cdr procedure takes as a parameter a non-empty list and returns the remaining elements (that is, all but the first element). In a sense, car and cdr are the inverses of cons; if you think of a non-empty list as having been assembled by a call to the cons procedure, car gives you back the first argument to cons and cdr gives you back the second one. 4

> (define colors (cons "red" (cons "black" nil))) > colors ("red" "black") > (car colors) "red" > (cdr colors) ("black") We can, and often do, nest these calls. For example, to get the second element of a list, we first get the cdr of the list and then take the car of that result. > (car (cdr colors)) "black" To reduce the amount of typing necessary for the programmer, many implementations of Scheme provide procedures that combine car and cdr in various ways. For example, cadr computes the car of the cdr of a list (the second element), cddr computes the cdr of the cdr of a list (all but the first two elements), and caar computes the car of the car of a list. > (define spots(list (list 10 1 color.red) (list 10 2 color.red) (list 10 3 color.red) (list 11 2 color.grey) (list 12 3 color.grey))) > spots ((10 1-16776961) (10 2-16776961) (10 3-16776961) (11 2-1465341697) (11 3-2139062017)) > (car spots) (10 1-16776861) > (cadr spots) (10 2-16776961) > (caddr spots) (10 3-16776961) > (cadddr spots) (11 2-1465341697) > (caar spots) 10 > (cadar spots) 1 > (caddar spots) -16776961 List Predicates Scheme provides two basic predicates for checking whether a value is a list. The null? predicate checks whether a value is the empty list. The list? predicate checks whether a value is a list (empty or nonempty). > (null? nil) #t > (list? nil) #t > (null? (list 1 2 3)) #f > (list? (list 1 2 3)) 5

#t > (null? 5) #f > (list? 5) #f Other Common List Procedures It turns out that you can build any other list procedure with just nil, cons, car, cdr, and nil?. Nonetheless, there are enough common operations that most programmers want to do with lists that Scheme includes them as basic operations. (That means you don t have to define them yourself.) Here are four that programmers frequently use. length The length procedure takes one argument, which must be a list, and computes the number of elements in the list. (An element that happens to be itself a list nevertheless contributes 1 to the total that length computes, regardless of how many elements it happens to contain.) > (length nil) 0 > (length (list 1 2 3)) 3 > (length (list (list 1 2 3))) 1 reverse The reverse procedure takes a list and returns a new list containing the same elements, but in the opposite order. > (reverse (list "white" "grey" "black")) ("black" "grey" "white") append The append procedure takes any number of arguments, each of which is a list, and returns a new list formed by stringing together all of the elements of the argument lists, in order, to form one long list. > (append (list "red" "green") (list "blue" "yellow")) ("red" "green" "blue" "yellow") The empty list acts as the identify for append. > (append nil (list "blue" "yellow")) ("blue" "yellow") > (append (list "red" "green") nil) ("red" "green") > (append nil nil) 6

list-ref The list-ref procedure takes two arguments, the first of which is a list and the second a non-negative integer less than the length of the list. It recovers an element from the list by skipping over the number of initial elements specified by the second argument (applying cdr that many times) and extracting the next element (by invoking car). So (list-ref sample 0) is the same as (car sample), (list-ref sample 1) is the same as (car (cdr sample)), and so on. > (define rainbow (list "red" "orange" "yellow" "green" "blue" "indigo" "violet")) > (list-ref rainbow 1) "orange" > (list-ref rainbow 0) "red" > (list-ref rainbow 5) "indigo" > (list-ref rainbow 7) Error: -- list-ref "Too few elements in list." Copyright 2007 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. 7