rules of processing lists announcements
play

Rules of Processing Lists Announcements HW3 is tougher than was - PowerPoint PPT Presentation

Rules of Processing Lists Announcements HW3 is tougher than was HW2; plan to spend a little more time on it. Monday was the last day that Ill be sharing the lecture- capture. Itll be back near end of semester, for review, etc.


  1. Rules of Processing Lists

  2. Announcements • HW3 is tougher than was HW2; plan to spend a little more time on it. • Monday was the last day that I’ll be sharing the lecture- capture. • It’ll be back near end of semester, for review, etc. • End of class feedback: on a scrap of paper, feel free to write down anything that left you confused/unhappy/whatever, and hand it to the TA at the door. (If class seemed OK, no need to do anything!)

  3. Evaluation examples i. (last class) (define (add1 x) (+ x 1)) (+ (add1 3) (add1 4)) • Why? • T o understand subtleties in material later today • T o make it easy to write the Rackette project

  4. From the Rackette handout • “Practice evaluating Rackette expressions by hand. Specifjcally, write out your evaluations for the ``Practice on Paper'' section of this assignment and bring them to your Design Check.” • “For practice, process the following programs in either example's style, starting from the usual initial top-level environment:” i. (let ((x 19)) (+ x 32)) ii. ((lambda (x) (/ x 10)) 100)

  5. Evaluation examples ii. Today (quickly!) (define (add1 x) (+ x 1))

  6. Name login QUIZ <your answers here > • [Quiz contents deleted]

  7. Quick additions: and and or <and-expr> := ( and <expr> <expr> <expr>* ) • Idea: ( and x y) is true only if x and y are both true • Formalized a little ( and x y) evaluates to true only if x and y both evaluate to true • Rule (the "and" rule) to evaluate (and b1 b2 b3 … bn) we • Evaluate b1, whose value must be a boolean (or it's an error) • If b1's value is false , then the and-expression's value is false and we are done. • Otherwise, we evaluate b2; if b2's value is false , then the and- expression's value is false and we are done. • Otherwise, we continue in this manner until bn; if all the expressions have evaluated to true , then the and-expression's value is true.

  8. Quick additions: and and or <or-expr> := ( or <expr> <expr> <expr>* ) • Idea: ( or x y) is true if either x or y or both are true • Formalized a little ( or x y) evaluates to true only if at least one of x or y evaluates to true • Rule (the "or" rule) to evaluate ( or b1 b2 b3 … bn) we • Evaluate b1, whose value must be a boolean (or it's an error) • If b1's value is true , then the or-expression's value is true and we are done. • Otherwise, we evaluate b2; if b2's value is true, then the or-expression's value is true and we are done. • Otherwise, we continue in this manner until bn; if all the expressions have evaluated to false , then the or-expression's value is false.

  9. Notes • and and or are keywords • "and" and "or" are not procedures • The procedure-application-expression rule says that you "evaluate all the arguments", but the and- and or-rules say "evaluate them one at a time, and if you get a certain result, then don't evaluate any more of them!" • Called short-circuiting • You'll see later on why it's built in •

  10. Notes • One more boolean thing: the "not" procedure: • (not true ) => false • (not false ) => true • Activity: write “not

  11. Design-recipe steps already done for you! ;; Data definition: ;; bool: true, false ;; ;; not : bool -> bool ;; input: ;; a boolean, b ;; output ;; a boolean; if b is true, output is false, and vice versa ;; (define (not b) … ) (check-expect (not true) false)

  12. Solution ;; Data definition: ;; bool: true, false ;; ;; not : bool -> bool ;; input: ;; a boolean, b ;; output ;; a boolean; if b is true, output is false, and vice versa ;; (define (not b) (if b false true)) (check-expect (not true) false)

  13. Last syntax piece for a while: if <if-expr> := ( if <expr> <expr> <expr> ) • Idea: ( if condition x y) is x if condition is true, y if it's false. • Rule (the "if" rule) to evaluate ( if condition yes-result no-result) we • Evaluate condition , whose value must be a boolean (or it's an error) • If the value is true , we evaluate yes-result, whose value is the value of the if-expression • If the value is false , we evaluate no-result, whose value is the value of the if-expression • NB for those familiar with other languages: An if- expression is an expression , not a "statement"; it has a

  14. Activity Write an expression involving x and y which evaluates to 37 if x is larger than 5, or y is less than 10, and evaluates to -1 otherwise.

  15. Activity Write an expression involving x and y which evaluates to 37 if x is larger than 5, or y is less than 10, and evaluates to -1 otherwise. (if (or (> x 5) (< y 10)) 37 -1) (if (or (> x 5) (< y 10)) 37 -1)

  16. Lists • Racket is derived from Scheme which is derived from LISP • LISP stands for “LISt Processing language” • Abstraction of lists: • A list can be empty (no items) or • There’s a fjrst item, and the rest of the list • You can add a new fjrst item (lengthening the list) •

  17. T erminology • atomic data: num, bool, string • compound data: lists!

  18. Lists • There are two ways to build a list, indeed, two difgerent kinds of lists. • First “constructor”: empty • Builds a list that corresponds to the notion of a list with no items in it. • Along with this comes a predicate, empty? • No surprises: (empty? empty) => true • Note: empty is a keyword ; you cannot defjne “empty”! • Also can’t use it as the name of an argument to a function… or any kind of name!

  19. Keywords so far • define • and • or • if • cond • else • true • false • empty

  20. The other way to create a list: cons • “cons” is short for “construct” cons: item * list -> list • How could we use cons right now? (cons 13 …) • Need a list to fjll in for “…” • We only have one choice: empty! (cons 13 empty)

  21. T ype-predicate? • Yes, it’s called “ cons?” as expected (cons? empty) => false (cons? (cons 13 empty)) => true

  22. First and rest – deconstructing a list • (first empty) … error • (rest empty) … error • (first (cons 3 (cons 4 empty)) ) => 3 • (rest (cons 3 (cons 4 empty))) => (cons 4 empty) • General rule: • (first (cons a b)) => a • (rest (cons a b)) => b

  23. Activities • Construct a list containing 2 and 3 in that order (so that “fjrst” of the list is 2). • What is (first (cons 4 (cons 3 empty))) ? • What is (rest (cons 2 empty)) ? • Build a list z containing (cons 2 empty) and (cons 3 empty) , in that order • What is (first (rest z)) ? • What is (first (first z)) ?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend