Control Operations - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Control Operations

Description:

letrec (letrec ((sum (lambda (ls) (if (null? ls) 0 ( (car ls) (sum (cdr ls) ... (lambda (n) (let fib ((i n)) (cond ((= i 0) 0) ((= i 1) 1) (else ( (fib (- i 1) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 14
Provided by: dav80
Category:

less

Transcript and Presenter's Notes

Title: Control Operations


1
Control Operations
  • Procedure Application
  • (procedure exp )
  • Sequencing
  • (begin exp1 exp2 )

2
Control Operations (cont.)
  • Conditionals
  • (if test consequent alternative)
  • (not exp)
  • (and exp )
  • (or exp )
  • (cond clause1 clause2 )
  • (case exp clause1 clause2 )

3
Local Binding
  • Can we do this?
  • (let ((sum (lambda (ls)
  • (if (null? ls)
  • 0
  • ( (car ls) (sum (cdr ls))))))))

4
Local Binding
  • Forms of let
  • (let ((var val) ) exp )
  • (let ((var val) ) exp )
  • (letrec ((var val) ) exp )
  • (let name ((var val) ) exp )

5
letrec
  • (letrec ((sum (lambda (ls)
  • (if (null? ls)
  • 0
  • ( (car ls) (sum (cdr ls))))))))

6
Named let example
  • (define fibonacci
  • (lambda (n)
  • (let fib ((i n))
  • (cond
  • (( i 0) 0)
  • (( i 1) 1)
  • (else (
  • (fib (- i 1))
  • (fib (- i 2))))))))

7
do Iteration
  • (do ((var val update) )
  • (test res ) exp )
  • (define factorial
  • (lambda (n)
  • (do ((i n (- i 1)) (a 1 ( a i)))
  • ((zero? i) a))))

8
for-each mapping
  • (for-each procedure list1 list2 )
  • similar to map except that
  • does not return a list of results
  • application of procedure to elements of the lists
    occurs in order from left to right.

9
Predicates
  • (eq? obj1 obj2) identical
  • (eqv? obj1 obj2) equivalent
  • (equal? obj1 obj2) same

10
List procedures
  • (list obj )
  • (list? obj)
  • (length list)
  • (list-ref list n)
  • (list-tail list n)
  • (memq obj list)
  • (memv obj list)
  • (member obj list)

11
Numbers
  • Tons of procedures that operate on numbers
  • - /
  • zero? positive negative? even?
  • modulo truncate round abs max min
  • sin cos tan sqrt exp log
  • number-gtstring string-gtnumber

12
Strings
  • string? stringlt? stringgt? stringlt? stringgt?
  • string-ci? string-cilt? string-cigt? string-cilt?
    string-cigt?
  • make-string string-length
  • string-copy string-append substring string-gtlist
    list-gtstring

13
Vectors
  • Vectors are basically lists with some
    optimization for referencing arbitrary elements
    in constant time.
  • Vectors begin with ( and end with )
  • Procedures
  • vector vector-length vector-ref vector-set!
  • vector-gtlist list-gtvector
Write a Comment
User Comments (0)
About PowerShow.com