Scheme Continued - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Scheme Continued

Description:

I suggest that you have DrScheme open to try out the examples that are discussed. ... Finish writing and testing functions in Class03 Scheme and Recursion.doc. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 10
Provided by: davidh127
Category:

less

Transcript and Presenter's Notes

Title: Scheme Continued


1
Scheme Continued
  • Based on notes by John E. Savage
  • I suggest that you have DrScheme open to try out
    the examples that are discussed.

2
Recursion
  • Finish writing and testing functions in Class03
    Scheme and Recursion.doc. These were started in
    class03.scm.
  • In the process of doing the above, introduce
    tracing and local variables.

3
Tracing
  • (display identifier) (newline)
  • (display (list s1 s2 s3)) (newline)
  • places blank between s-expressions
  • surrounds with ( )
  • (display s1) (display s2) (display s3) (newline)
  • places no blank between s-expressions
  • s-expression could be a string such as "junk "

4
Local Variables
  • (let ltbindingsgt ltbodygt)
  • ltbindingsgt of form ((ltvar1gt ltinit1gt) )
  • ltinitigt evaluated in external environment
  • value of last expression in ltbodygt is returned
  • gt (let ((x 2) (y 3)) ( x y))
  • 6
  • gt (let ((x 2) (y 3))
  • (let ((x 7) (z ( x y))) z evaluated with
    x2, y3
  • ( z x)))
  • 35
  • (letrec ltbindingsgt ltbodygt)
  • same as let except ltinitigt evaluated with local
    values of ltvarigt if possible

5
More on Local Variables
  • (letrec
  • ((even?
  • (lambda (n)
  • (if (zero? n) t (odd? (sub1 n)))))
  • (odd?
  • (lambda (n)
  • (if (zero? n) f (even? (sub1 n))))))
  • (even? 88)
  • t
  • even? defined in terms of odd? which is defined
    in terms of even?
  • even? and odd? are local to the scope of letrec

6
Questions Answered
  • Can Scheme handle nondecimal numbers?
  • Numbers are input and displayed in base 10.
    However, it is possible to convert between
    decimal and bases 2, 8, or 16 as shown
  • gt (string-gtnumber AF36 16)
  • 44854
  • gt (number-gtstring 44854 16)
  • "af36
  • Why do I get parentheses and periods in my
    output?
  • You may be consing two atoms togethergt (cons a
    b)(a . b)
  • Rather than consing an atom to a listgt (cons a
    (b))(a b)

7
Questions Answered
  • Is Scheme polymorphic?
  • No
  • gt (define fun (lambda (x) ( x x)))
  • gt (define fun (lambda (x y) ( x y)))
  • gt (fun 4)
  • procedure fun expects 2 arguments, given 1 4
  • Then how can Scheme handle functions with an
    indeterminate number of arguments?
  • In the lambda statement, use a single argument
    without surrounding parentheses. This formal
    parameter is bound to a list of arguments
    passed.gt (define fun (lambda x x))gt (fun 2 3
    4)(2 3 4)

8
caadar and Helper Functions
  • The value function
  • gt (value '(3 (4 5)))
  • 23
  • gt (value '(((1 - 7) / 3) (4 5)))
  • 18
  • Helper Functions
  • (define 1st-sub-exp (lambda (aexp) (car aexp)))
  • (define operator (lambda (aexp) (cadr aexp)))
  • (define 2nd-sub-exp (lambda (aexp) (caddr aexp)))
  • Implementation
  • (define value
  • (lambda (aexp)
  • (cond
  • ((atom? aexp) aexp)
  • ((eq? (operator aexp) ')
  • ( (value (1st-sub-exp aexp)) (value
    (2nd-sub-exp aexp))))
  • ((eq? (operator aexp) '-)
  • (- (value (1st-sub-exp aexp)) (value
    (2nd-sub-exp aexp))))
  • ((eq? (operator aexp) ')

9
If there is time . . .
  • Note thelittleschemer.scm resource.
  • Implement some set theory functions for sets of
    atoms or for sets that may include sets as
    elements.
Write a Comment
User Comments (0)
About PowerShow.com