roary-schultz
Uploaded by
19 SLIDES
335 VUES
190LIKES

Understanding Even and Odd Number Filtering in Scheme Programming

DESCRIPTION

This lecture explores the implementation of filtering even and odd numbers from lists in Scheme programming. It covers the use of predicates, such as `even?` and `odd?`, and showcases recursive procedures for manipulating lists. The presentation includes clicker questions for interactive learning, allowing participants to engage with concepts like filtering sentences and utilizing lambda expressions. The lecture aims to deepen understanding of Scheme’s list processing capabilities and encourages attendees to practice writing their own procedures.

1 / 19

Télécharger la présentation

Understanding Even and Odd Number Filtering in Scheme Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS61A Lecture 4 2011-06-23 Colleen Lewis

  2. Clicker Test  Do you have a piazzza account? Yes No

  3. Remove all non-even numbers from a sentence (evens ’( 2 8 3 )) (evens ’( 8 3 )) (se 2 (se 8 (evens ’( 3 )) (se (evens ’()) STk>(evens ’(2 8 3)) (2 8) ’() (se 2 (se 8 (se ’())))

  4. Remove all non-even numbers from a sentence (evens ’( 3 9 6 )) (evens ’( 9 6)) (se (se (evens ’( 6 )) (se 6 (evens ’()) STk>(evens ’(3 9 6)) (6) ’() (se (se (se 6 ’())))

  5. Write evens A)easy B)medium C)hard D)stuck ;Assume you have the predicate even? (define (even? x) (= 0 (remainder x 2)))

  6. evensSolution ______) ______) ______) (define (evens sent) (cond ((empty? sent) ’()) ((even? (first sent)) (se (first sent) (evens (bf sent)))) (else (se(evens (bf sent))))))

  7. odds Solution odds odd? odds odds (define (evens sent) (cond ((empty? sent) ’()) ((even? (first sent)) (se (first sent) (evens (bf sent)))) (else (evens (bf sent)))))

  8. pronouns Solution pronouns pronouns pronouns pronoun? (define (evens sent) (cond ((empty? sent) ’()) ((even? (first sent)) (se (first sent) (evens (bf sent)))) (else (evens (bf sent)))))

  9. keep Solution 1 keep keep keep pred? (define (evens sent) (cond ((empty? sent) ’()) ((even? (first sent)) (se (first sent) (evens (bf sent)))) (else (evens (bf sent)))))

  10. keep Solution 2 pred? (define (keep _______ sent) (cond ((empty? sent) ’()) ((pred? (first sent)) (se (first sent) (keep pred? (bf sent)))) (else (keep pred?(bf sent)))))

  11. Calling keep (define (keep pred? sent) (cond ((empty? sent) ’()) ((pred? (first sent)) (se (first sent) (keep pred? (bf sent)))) (else (keep pred? (bf sent))))) STk> (keep even? ’(2 8 3)) (2 8)

  12. Calling keep (keep odd? ’(1 4 8 5)) (keep pronoun? ’(I me not you)) (keep even? ’(1 3 5 7)) (keep (> 5) ’(1 3 5 7)) AHHHH! Not a function!!! STk> (1 5) STk> (I me you) STk> () STk>

  13. keep (keepprocedure sent) • procedure • a procedure that takes in one argument • a procedure that returns #t or #f • sent • a sentence with 0 or more words • a word with 0 or more letters

  14. Try it! Make this work! (lambda (x) (> x 5)) A)easy B)medium C)hard D)stuck STk>(keep ______________ ’(8 3 4 7)) (8 7) Talk to your partner about why (> 5) doesn’t work!!! Try to write it without a helper method

  15. every (everyprocedure sent) • procedure • a procedure that takes in one argument • a procedure that returns a word or a sentence • sent • a sentence with 0 or more words • a word with 0 or more letters

  16. Try it! Make this work! (lambda (x) (+ x 10)) A)easy B)medium C)hard D)stuck STk>(every ______________ ’(8 3 4 7)) (18 13 14 17) Talk to your partner about why (+ 10) doesn’t work!!! Try to write it without a helper method

  17. Try it! Define s-g-t-100 Squares-greater-than-100 STk>(s-g-t-100 '(2 9 13 16 9 45)) (169 256 2025) (define (s-g-t-100 sent) (keep (lambda (x) (> x 100)) (every (lambda (y) (* y y)) sent)))

  18. Using lambda with define These are VERY DIFFERENT: (define (cat y) (lambda (x) (+ x 1))) (define dog (lambda (x) (+ x 1)))

  19. Using lambda with define (define (cat y) (lambda (x) (+ x 1))) (define dog (lambda (x) (+ x 1))) Which of these gives the answer 2? I (cat 1) II ((cat 1) 1) III(dog 1) IV ((dog 1) 1) A) I&III B) II&IV C) I&IV D) II&III E) Not sure Correct Answer

More Related