TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.

Slides:



Advertisements
Similar presentations
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Lab1 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 7 part 1 By Herb I. Gross and Richard A. Medeiros next.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Dec 7, What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel Notes heavily borrowed from Steve Wolfman’s,
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
Binary Conversion In today’s lesson we will link together the binary and algorithm topics by looking at how to get the computer to: convert binary to decimal.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Subsets. Subsets are sort of like nested Russian dolls: the subset “fits inside” the set.
Adapted from slides by Marie desJardins
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Lists in Python.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Common lisp A functional programming language. Useful URL:
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Computabilty Computability Finite State Machine. Regular Languages. Homework: Finish Craps. Next Week: On your own: videos +
CS 103 Discrete Structures Lecture 10 Basic Structures: Sets (1)
Ways to Check for Divisibility Dividing By 1 All numbers are divisible by 1.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
TeachScheme, ReachJava Adelphi University Thursday morning July 15, 2010.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
Counting Techniques. L172 Agenda Section 4.1: Counting Basics Sum Rule Product Rule Inclusion-Exclusion.
Ways to Check for Divisibility Dividing By 1 All numbers are divisible by 1.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Factorization : Difference of Two Squares and Perfect Squares.
Ways to Check for Divisibility Dividing by 2 All even numbers are divisible by 2 Even numbers are numbers that end with either 2, 4, 6, 8, or 0.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
CS314 – Section 5 Recitation 10
Generalizing Similar Functions
Class 11: Two-argument recursion
Case Study: Undefined Variables
(early slides assume map/filter)
Let's Play "What's the Question"
Developing Programs for Family Trees
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Presentation transcript:

TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008

Review definition by choices definition by parts char, symbol, key & mouse handlers posns defining other structs definition by choices, revisited (unions) lists (2 choices, one of which has 2 parts, one of which is the orig. type) structural recursion: apply already-learned coding patterns to self-referential type

Lists A list is either empty, or (cons element-type list) (define (function-on-list L) (cond [(empty? L) … ] [(cons? L) ; (first L) element-type ; (rest L) list ; (function-on-list (rest L)) whatever

Counting ; count-dolls : list-of-strings -> number "Examples of count-dolls:" (check-expect (count-dolls empty) 0) (check-expect (count-dolls (cons "ball" empty)) 0) (check-expect (count-dolls (cons "doll" empty)) 1) (check-expect (count-dolls (cons "ball" (cons "doll" (cons "doll" (cons "bat" (cons "doll" empty)))))) 3) You try this one. Hint: you'll need a "cond" inside the "cons?" case.

My answer ; count-dolls : list-of-strings -> number (define (count-dolls toys) (cond [(empty? toys) ; toys an empty list 0 ; from one of the test cases ] [(cons? toys) ; toys a non-empty list of strings ; (first toys) a string ; (rest toys) a list of strings ; (count-dolls (rest toys)) a number ; (string=? (first toys) "doll") a boolean (cond [(string=? (first toys) "doll") (+ 1 (count-dolls (rest toys)))] [else (count-dolls (rest toys))]) ]))

A slightly different approach ; count-dolls : list-of-strings -> number (define (count-dolls toys) (cond [(empty? toys) ; toys an empty list 0 ; from one of the test cases ] [(cons? toys) ; toys a non-empty list of strings ; (first toys) a string ; (rest toys) a list of strings ; (count-dolls (rest toys)) a number (+ (1-if-doll (first toys)) (count-dolls (rest toys))) ])) ; "helper" function 1-if-doll : string -> number ; returns 1 if the string is "doll", 0 if it's anything else.

A simpler syntax ; list : object … -> list (list "ball" "doll" "doll" "bat" "doll") is equivalent to (cons "ball" (cons "doll" (cons "doll" (cons "bat" (cons "doll" empty))))) Set Language -> Beginning Student with List Abbreviations Now output will be in "list" syntax rather than "cons" syntax No internal difference; any function that worked before will still work

List exercises Write a function add-positives which takes in a list of numbers and computes the sum of the positive ones, ignoring any zeroes or negatives. Write a function largest which takes in a non-empty list of numbers and returns the largest of them. (Hint: need to define new data type "non-empty list of numbers". Either one element, i.e. (cons # empty), or more than one, i.e. (cons # nelon).)

Functions producing lists Write a function add-1-to-each which takes in a list of numbers and produces a list of numbers, each 1 more than the corresponding number in the input. Write a function convert-grades which takes in a list of numeric grades and produces a list of corresponding letter grades ("A", "B", etc.) in the same order. Write a function substitute which takes in two strings (old and new) and a list of strings, and produces the same list with all occurrences of old replaced with new.

Functions producing lists Write a function keep-positives which takes in a list of numbers and returns a list of only the positive numbers in the list, in the same order, omitting negatives and zeroes. Write a function sort which takes in a list of numbers and returns a list of the same numbers in increasing order. (Hint: don't worry about "what sorting algorithm"; just follow the recipe. You'll need an auxiliary function; again, just follow the recipe.)

Extra credit: lists of lists Write a function permutations which takes in a list (of anything) and returns a list of lists, comprising all the possible orderings of the original list. For example, (check-expect (permutations (list 1 2 3)) (list (list 1 2 3) (list 1 3 2) (list 2 1 3) (list 2 3 1) (list 3 1 2) (list 3 2 1))) Write a function subsets which takes in a list (of anything) and returns a list of lists, comprising all the subsets of the original list (don't worry about order). For example, (check-expect (subsets (list 1 2 3)) (list (list) (list 3) (list 2) (list 2 3) (list 1) (list 1 3) (list 1 2) (list 1 2 3)))

Lists of structs An employee-list is either empty, or (cons employee employee-list) An employee is… See employees.scm as starting point Since an emp-list has a part which is an emp, function-on-emp-list calls function-on-emp on that part

Lists of structs Write total-salary which takes in a list of employees & adds up their salaries Write any-over-100k? which takes in a list of employees & tells whether any of them earn over $100,000/year Write count-over-100k which takes in a list of employees & tells how many of them earn over $100,000/year Write give-10%-raises which takes in a list of employees & returns a similar list in which everybody's salary has increased by 10% Write fire-over-100k which takes in a list of employees & returns a list of the ones who don't earn over $100,000/year. Write highest-paid, which takes in a non-empty list of employees & returns the highest-paid one. (If there's a tie, return the first of the highest-paid employees.)

Lists of unions An animal-list is either empty, or (cons animal animal-list) An animal is either a dog, or a fish A dog is… A fish is… See animals.scm as a starting point Since a list of animals has a part which is an animal, function-on- animal-list calls function-on-animal (which in turn calls function-on-dog, function-on-fish, etc.)

Lists of unions Write count-tigers, which takes in a list of animals and returns how many of them are tigers Write extract-fish, which takes in a list of animals and returns a list of only the fish, in the same order they were in before Write lookup-age, which takes in a list of animals and a string, and if there is a dog with that name in the list, returns the dog's age. (If there's more than one, use the first one you find.) If not, return -1.