day10

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

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
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 10: October 29, 2008 More list processing Caltech CS 1 -- Fall 2008 Plan 9 From Outer Space (1959) Caltech CS 1 -- Fall 2008 List processing in Scheme Last time: Introduced lists Worked with lists growing lists recursing over lists creating lists from lists Caltech CS 1 -- Fall 2008 Today Revisit design strategy for writing list processing procedures Procedures that take in and return lists deletion insertion Sorting procedures an as sort ment of them Caltech CS 1 -- Fall 2008 Summary of design strategy Identify the inputs and outputs Write contract, comment, skeleton What are the base cases ? and what do I do with them? Otherwise, what should I do with the first element of the list? the rest of the list? and how do I combine to get final result? Caltech CS 1 -- Fall 2008 Recall: What is a "base case"? What is the definition of a base case ? Something you can solve... without... having to make any recursive calls i.e. something you can solve immediately Often represents a structural feature (empty list) (can also represent other things) Can have multiple base cases Easy to find, easy to write code for Caltech CS 1 -- Fall 2008 Example: list manipulations Make a new list Like an old list But slightly different e.g. Add a new element to front (last time) Remove an element Insert an element in a particular place Caltech CS 1 -- Fall 2008 Result data structure (define (make-result x y) (cons x y)) (define (get-x result) (car result)) (define (get-y result) (cdr result)) e.g. (define a-result (make-result 3 4)) D a y 9 a-result Caltech CS 1 -- Fall 2008 Removing from a list Remember our experiment from last time list of value, result pairs ( x , y pairs) Maybe you decide the x=2 point was an error my-results Caltech CS 1 -- Fall 2008 Remove element Problem: Want to remove first element from the list that has a particular x value Inputs: a-list , x Output: new list, without the element containing the offending x value Contract: remove-by-x: list-of results number -> list-of results Caltech CS 1 -- Fall 2008 Remove element Contract / comment / skeleton: ;; remove-by-x: list-of results number -> list-of results ;; This function returns a new list of results, with the ;; first result that has a particular x value removed. ;; The original list is not altered. (define (remove-by-x a-list x) ) Caltech CS 1 -- Fall 2008 Remove element: base cases What are the base cases? What to do if list is empty? return list unchanged (define (remove-by-x a-list x) (cond ((null? a-list) a-list) ...) ) Caltech CS 1 -- Fall 2008 Remove element: base cases Any other base cases?...
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 / 106

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online