day09

# day09 - Caltech CS 1 -- Fall 2008 CS1: Introduction to...

This preview shows pages 1–20. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 9: October 27, 2008 List Processing (Special pre-Halloween Lecture) I ♥ CS 1! Caltech CS 1 -- Fall 2008 Today • More new Scheme constructs – type predicates, error • New data type: lists • Extended example – collecting experimental data Caltech CS 1 -- Fall 2008 Type Predicates • pair? – Scheme function that tells us if argument is a cons cell (pair? (cons 3 4)) #t (pair? 3) #f Caltech CS 1 -- Fall 2008 Other Type Predicates • (number? x) #t for integers, rationals, reals, … #f for non-numbers • (boolean? x) #t for #t or #f #f for anything else • (procedure? x) you get the idea Caltech CS 1 -- Fall 2008 error • Sometimes, things go wrong during a computation • Need way to abort computation early • Use the error function Caltech CS 1 -- Fall 2008 error (define (factorial n) (cond ((< n 0) ( error "factorial -- invalid n")) ((= n 0) 1) (else (* n (factorial (- n 1)))))) Caltech CS 1 -- Fall 2008 error (factorial -1) factorial -- invalid n • Aborts entire computation, prints message, returns to interpreter Caltech CS 1 -- Fall 2008 error • Can also add data to print when error happens (define (factorial n) (cond ((< n 0) ( error "factorial -- invalid n: " n )) ((= n 0) 1) (else (* n (factorial (- n 1)))))) Caltech CS 1 -- Fall 2008 error (factorial -1) factorial -- invalid n: -1 • Provides more information to user Caltech CS 1 -- Fall 2008 On to lists ... Caltech CS 1 -- Fall 2008 Common situation • Need to represent an ordered sequence of data • So far, know how to create pairs – and pairs of pairs • and pairs of pairs of pairs... • How to exploit this to create a "list" of data? Caltech CS 1 -- Fall 2008 nil • First designate a special "end of list" value – generally called nil – We'll represent as a box with a slash through it: nil Caltech CS 1 -- Fall 2008 nil and null? • Can generate nil value by calling the function list with no arguments: (list) – i.e. nil is the "empty list" • Can define nil if we want to: (define nil (list)) –but not really necessary • To test for nil , use null? – returns #t if argument is nil, #f otherwise Caltech CS 1 -- Fall 2008 Note • The name nil is not predefined in DrScheme – so don't use it unless you define it yourself • Usually we just use (list) instead • Sometimes I'll say nil in this lecture anyway for convenience Caltech CS 1 -- Fall 2008 Lists • Can create list of items by joining together cons pairs ending in nil : Caltech CS 1 -- Fall 2008 Lists • Can create list of items by joining together cons pairs ending in nil : Caltech CS 1 -- Fall 2008 Lists • Can create list of items by joining together cons pairs ending in nil : Caltech CS 1 -- Fall 2008 Lists • Can create list of items by joining together cons pairs ending in nil : Caltech CS 1 -- Fall 2008 Lists • Can define a list in Scheme like this: (define our-list (cons 1 (cons 2 (cons 3 nil))) ) our-list (cons 1 (cons 2 (cons 3 nil))) Caltech CS 1 -- Fall 2008...
View Full Document

## This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

### Page1 / 125

day09 - Caltech CS 1 -- Fall 2008 CS1: Introduction to...

This preview shows document pages 1 - 20. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online