rules of processing short circuiting rules the last rule
play

Rules of Processing Short-Circuiting Rules The last rule of - PowerPoint PPT Presentation

Rules of Processing Short-Circuiting Rules The last rule of evaluation (for now) (define (f x) x) (f 4) The goal of the proc-app-expr rule Subtle questions Didnt you say we couldnt define things twice? Isnt that what the


  1. Rules of Processing Short-Circuiting Rules

  2. The last rule of evaluation (for now) (define (f x) x) (f 4)

  3. The goal of the proc-app-expr rule

  4. Subtle questions • “Didn’t you say we couldn’t define things twice? Isn’t that what the proc-app-expr rule is doing?” • Yes. But I used “define” very specifically: the rules say that if, in a definition , the name is already bound to a value, it’s an error • There are other ways that names get bound to values (i.e., the proc-app-expr rule)! • No, that’s not what the proc-app-expr rule is doing. • “How are values in Racket stored? Directly, or by reference? • Not a question most of you should be ready to ask, or to even understand! • Answer: It’s not specified, because it doesn’t matter . • That lets you, the Racket implementor, make whatever choice is easiest for you! • The power of abstraction: ignoring the parts that don’t matter gives you more power.

  5. A new bit of syntax: cond

  6. Cond-expression structure

  7. Cond example (define x 5) (cond [(negative? x) “negative”] [(positive? x) “positive”] [(zero? x) “zero”]) The first test produces “false”; we move on. The second test produces “true”; we evaluate the result to get “positive”. We’re done; we never evaluate the third test. If we’d defined x to be 0, we’d have gotten “zero”.

  8. Base-pairs • In biology, there are things called “bases” and named A, T, C, G • They come in “complementary pairs”: A and T are complements, C and G are complements • We’ll represent these with strings, “A”, ”T”, “C”, “G”. • Write a procedure “complement” that consumes a base and produces the complementary base

  9. ;; Data definition ;; string: "abs", "T"

  10. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;;

  11. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;; (define (complement base) …)

  12. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;; ;; input: ;; base, a string, which must be one of "A", "T", "C", "G" ;; output: ;; the complementary base, one of "A", "T", "C", "G" (define (complement base) …)

  13. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;; ;; input: ;; base, a string, which must be one of "A", "T", "C", "G" ;; output: ;; the complementary base, one of "A", "T", "C", "G" (define (complement base) …) (check-expect (complement "A") "T") (check-expect (complement "T") "A") (check-expect (complement "C") "G") (check-expect (complement "G") "C”)

  14. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;; ;; input: ;; base, a string, which must be one of "A", "T", "C", "G" ;; output: ;; the complementary base, one of "A", "T", "C", "G" (define (complement base) (cond [(string=? base "A") "T"] [(string=? base "T") "A"] [(string=? base "C") "G"] [(string=? base "G") "C"])) (check-expect (complement "A") "T") (check-expect (complement "T") "A") (check-expect (complement "C") "G") (check-expect (complement "G") "C")

  15. ;; input: ;; base, a string, which must be one ;; of "A", "T", "C", "G" General pattern here ;; ... (define (complement base) (cond [(string=? base "A") "T"] • The structure of the program follows [(string=? base "T") "A"] the structure of the data [(string=? base "C") "G"] [(string=? base "G") "C"])) • Repeated principle throughout CS17 • Almost always a good start when writing any program

  16. ;; Data definition ;; string: "abs", "T" ;; ;; complement: string -> string ;; ;; input: ;; base, a string, which must be one of "A", "T", "C", "G" ;; output: ;; the complementary base, one of "A", "T", "C", "G" (define (complement base) (cond What should this program produce when given [(string=? base "A") "T"] the input “B”? [(string=? base "T") "A"] [(string=? base "C") "G"] [(string=? base "G") "C"])) (check-expect (complement "A") "T") (check-expect (complement "T") "A") (check-expect (complement "C") "G") (check-expect (complement "G") "C")

  17. Evaluation examples i. (define (add1 x) (+ x 1)) (+ (add1 3) (add1 4))

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