CS3L:%20Introduction%20to%20Symbolic%20Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CS3L:%20Introduction%20to%20Symbolic%20Programming

Description:

(define (math-function? func) (or (equal? func ) (equal? func -) (equal? func ... t. STk (math-function ) #f ...and procedures can be returned from procedures ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 9
Provided by: collee2
Category:

less

Transcript and Presenter's Notes

Title: CS3L:%20Introduction%20to%20Symbolic%20Programming


1
CS3L Introduction to Symbolic Programming
Lecture 15 Procedures as Arguments
  • Summer 2008 Colleen Lewis
  • colleenL_at_berkeley.edu

2
Today
  • Homework
  • Mon Mini-project 2 due Thursday
  • Use the Modeler
  • Procedures as Arguments
  • Procedures returned from Procedures
  • Higher Order Functions (HOF)
  • Recursion Patterns

3
REALLY IMPORTANT!Use the Modeler!
  • The exercises that use the modeler are really
    helpful!
  • If you dont know how to do them ASK!

4
Procedures can be taken as arguments
  • (define (math-function? func)
  • (or (equal? func )
  • (equal? func -)
  • (equal? func )
  • (equal? func /)))
  • STkgt (math-function )
  • t
  • STkgt (math-function )
  • f

5
and procedures can be returned from procedures
  • (define (choose-func name)
  • (cond ((equal? name 'plus) )
  • ((equal? name 'minus) -)
  • ((equal? name 'divide) /)
  • (else 'sorry)))
  • STkgt ((choose-func 'plus) 3 5)
  • 8
  • STkgt ((choose-func 'minus) 3 5)
  • -2

6
Higher order function (HOFs)
  • A HOF is a function that takes a function as an
    argument.
  • (define (do-math f arg1 arg2)
  • (if (and (equal? arg2 0)
  • (equal? f /))
  • '(error divide by zero)
  • (f arg1 arg2)))
  • (do-math 10 3)
  • should I quote the -?

7
Patterns for simple recursions
  • Most recursive functions that operate on a
    sentence fall into
  • Mapping
  • Counting
  • Finding
  • Filtering
  • Testing
  • Combining

lt- every
lt- keep
lt- accumulate
8
Common HOFs
  • There are three main ones that work with words
    and sentences
  • every - do something to each element
  • keep - return only certain elements
  • accumulate - combine the elements
  • STkgt (every last (hi how are you))
  • (i w e u)
Write a Comment
User Comments (0)
About PowerShow.com