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

whirlwind tour of racket
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Whirlwind Tour

  • f Racket

CS 115 | Saelee

1

slide-2
SLIDE 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

slide-3
SLIDE 3

Data

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

3

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 7

Q:

  • How are variables useful?
  • Why aren’t we allowed to redefine

variables?

7

slide-8
SLIDE 8

Arithmetic

  • “Primitive” operations: +, -­‑, *, /

(-­‑ ¡3 ¡4) ¡⇒ ¡-­‑1 (* ¡9 ¡1000) ¡⇒ ¡9000 (/ ¡1 ¡(* ¡3 ¡3)) ¡⇒ ¡0.̅ ¡1

8

slide-9
SLIDE 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

slide-10
SLIDE 10

Mathematical functions

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

10

slide-11
SLIDE 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

slide-12
SLIDE 12

Wait a sec…

  • define doesn’t produce a value, when

evaluated

  • It isn’t a function!
  • define is a “special form”

12

slide-13
SLIDE 13
  • 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!)

Referential Transparency

13

slide-14
SLIDE 14

Q:

  • Why is referential transparency good?
  • Would it ever make sense to not have

referential transparency?

14

slide-15
SLIDE 15

Comparisons & Booleans

  • Relational operators from math:
  • =, <, <=, >, >=
  • Expressions that uses these operators

evaluate to Boolean values

  • true or false

15

slide-16
SLIDE 16

Comparisons & Booleans

(> ¡10 ¡5) ¡ ¡ ¡ ¡ ¡⇒ ¡true (= ¡1 ¡2) ¡ ¡ ¡ ¡ ¡ ¡⇒ ¡false (<= ¡-­‑5 ¡2000) ¡⇒ ¡true

16

slide-17
SLIDE 17

Comparisons & Booleans

  • Also other functions for comparison
  • symbol=?, string=?, etc.

17

slide-18
SLIDE 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

slide-19
SLIDE 19

Boolean operations

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

19

slide-20
SLIDE 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

slide-21
SLIDE 21

Conditional expressions

(define ¡weather ¡'rainy) (cond ¡((symbol=? ¡weather ¡'sunny) ¡'bicycle) ¡ ¡ ¡ ¡ ¡ ¡((symbol=? ¡weather ¡'rainy) ¡'bus))

  • How to apply this easily to different values
  • f weather?

21

slide-22
SLIDE 22

Our own functions

  • E.g., so we can do

(transport-­‑mode ¡'sunny) (transport-­‑mode ¡'rainy)

22

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

Q:

  • Is cond referentially transparent?
  • What happens when no condition in a

cond evaluates to true? Why?

25

slide-26
SLIDE 26

(define ¡(transport-­‑mode ¡weather) ¡ ¡(cond ¡((symbol=? ¡weather ¡'sunny) ¡'bicycle) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡((symbol=? ¡weather ¡'rainy) ¡'bus) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡'car)))

Always have a default

(transport-­‑mode ¡'freezing) ¡⇒ ¡'car

26

slide-27
SLIDE 27

Drawing

  • See http://docs.racket-lang.org/teachpack/

2htdpimage.html for documentation of drawing functions

27

slide-28
SLIDE 28

Just for fun …

  • Draw the IIT logo (use triangles and lines)

28