whirlwind tour of racket
play

Whirlwind Tour of Racket CS 115 | Saelee 1 Whats in a name? - PowerPoint PPT Presentation

Whirlwind Tour of Racket CS 115 | Saelee 1 Whats in a name? Racket vs. Scheme vs. BSL vs. Different dialects of one language Slightly different features & grammar Ill (try to) always say Racket 2 Data


  1. Whirlwind Tour of Racket CS 115 | Saelee 1

  2. What’s in a name? • Racket vs. Scheme vs. BSL vs. … • Different dialects of one language • Slightly different features & grammar • I’ll (try to) always say “Racket” 2

  3. Data • “Stuff ” our programs work on • Can be used as input and output 3

  4. Atomic data • Discrete (logically indivisible) chunks of information • E.g., numbers: 0 , 1 , 2 , 3 , 4 , … characters: #\a , #\b , #\c , ... booleans: true , false symbols: 'red , 'wednesday 4

  5. Compound data • Can be split into constituent parts • E.g., strings: "hello ¡world" , lists: (list ¡1 ¡'fish ¡2 ¡'fish) • (string-­‑ref ¡"hello ¡world" ¡0) ⇒ #\h (first ¡(list ¡1 ¡'fish ¡2 ¡'fish)) ⇒ 1 5

  6. Named values • (define ¡mypi ¡3.14) binds the variable mypi to the constant 3.14 • Once defined, cannot re-define the value of a variable! 6

  7. Q: • How are variables useful? • Why aren’t we allowed to redefine variables? 7

  8. Arithmetic • “Primitive” operations: + , -­‑ , * , / (-­‑ ¡3 ¡4) ¡ ⇒ ¡-­‑1 (* ¡9 ¡1000) ¡ ⇒ ¡9000 (/ ¡1 ¡(* ¡3 ¡3)) ¡ ⇒ ¡0.̅ ¡1 8

  9. Mathematical functions • E.g., abs , sqr , sqrt , expt , sin • Many more provided by Racket • Use in the same way as primitive operators • In reality, “primitive operators” are just functions with symbols for names 9

  10. Mathematical functions (abs ¡-­‑7) ¡ ¡ ¡ ⇒ ¡7 (expt ¡2 ¡3) ¡ ⇒ ¡8 (sqrt ¡(+ ¡(sqr ¡3) ¡(sqr ¡4))) ¡ ⇒ ¡5 10

  11. Expressions • The application of functions to their inputs create expressions • e.g., (+ ¡(sqr ¡3) ¡(sqr ¡4)) • All expressions evaluate (or reduce ) to values 11

  12. Wait a sec… • define doesn’t produce a value, when evaluated • It isn’t a function! • define is a “special form” 12

  13. Referential Transparency • Fancy sounding but simple idea: • Applying a referentially transparent function to the same input(s) many times will always result in the same value • e.g., (+ 2 2) is always = 4 (duh!) 13

  14. Q: • Why is referential transparency good? • Would it ever make sense to not have referential transparency? 14

  15. Comparisons & Booleans • Relational operators from math: • = , < , <= , > , >= • Expressions that uses these operators evaluate to Boolean values • true or false 15

  16. Comparisons & Booleans (> ¡10 ¡5) ¡ ¡ ¡ ¡ ¡ ⇒ ¡true (= ¡1 ¡2) ¡ ¡ ¡ ¡ ¡ ¡ ⇒ ¡false (<= ¡-­‑5 ¡2000) ¡ ⇒ ¡true 16

  17. Comparisons & Booleans • Also other functions for comparison • symbol=? , string=? , etc. 17

  18. Boolean operations • For compound expressions, e.g., • time ≥ 1.5 hours or color ≠ pink • Boolean operations: and , or , not • Input: Boolean(s); Output: Boolean 18

  19. Boolean operations (define ¡time ¡1.8) (define ¡color ¡'pink) (or ¡(>= ¡time ¡1.5) ¡ ¡ ¡ ¡(not ¡(symbol=? ¡color ¡'pink))) 19

  20. Conditional expressions • Likely want to do different things based on value of a Boolean expressions • E.g., take bicycle if sunny, bus if rainy • Special form: cond 20

  21. Conditional expressions (define ¡weather ¡'rainy) (cond ¡((symbol=? ¡weather ¡'sunny) ¡'bicycle) ¡ ¡ ¡ ¡ ¡ ¡((symbol=? ¡weather ¡'rainy) ¡'bus)) • How to apply this easily to different values of weather ? 21

  22. Our own functions • E.g., so we can do (transport-­‑mode ¡'sunny) (transport-­‑mode ¡'rainy) 22

  23. Our own functions • Use define again, but like this: (define ¡(transport-­‑mode ¡weather) ¡…) • transport-­‑mode is our function name • inputs to function are bound to weather • … is the function body 23

  24. Our own functions (define ¡(transport-­‑mode ¡weather) ¡ ¡(cond ¡((symbol=? ¡weather ¡'sunny) ¡'bicycle) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡((symbol=? ¡weather ¡'rainy) ¡'bus))) (transport-­‑mode ¡'sunny) ¡ ⇒ ¡'bicycle (transport-­‑mode ¡'rainy) ¡ ⇒ ¡'bus 24

  25. Q: • Is cond referentially transparent? • What happens when no condition in a cond evaluates to true? Why? 25

  26. Always have a default (define ¡(transport-­‑mode ¡weather) ¡ ¡(cond ¡((symbol=? ¡weather ¡'sunny) ¡'bicycle) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡((symbol=? ¡weather ¡'rainy) ¡'bus) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡'car))) (transport-­‑mode ¡'freezing) ¡ ⇒ ¡'car 26

  27. Drawing • See http://docs.racket-lang.org/teachpack/ 2htdpimage.html for documentation of drawing functions 27

  28. Just for fun … • Draw the IIT logo (use triangles and lines) 28

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