Introducing Racket A brief tour of history We wanted a language - - PowerPoint PPT Presentation

introducing racket a brief tour of history we wanted a
SMART_READER_LITE
LIVE PREVIEW

Introducing Racket A brief tour of history We wanted a language - - PowerPoint PPT Presentation

Introducing Racket A brief tour of history We wanted a language that allowed symbolic manipulation Scheme The key to understanding LISP is understanding S-Expressions Racket List of either atoms or S-expressions (this (is an) (s)


slide-1
SLIDE 1

Introducing Racket

slide-2
SLIDE 2

λ

slide-3
SLIDE 3

A brief tour of history…

slide-4
SLIDE 4
slide-5
SLIDE 5

We wanted a language that allowed symbolic manipulation

slide-6
SLIDE 6

The key to understanding LISP is understanding S-Expressions Scheme Racket

slide-7
SLIDE 7

(this (is an) (s) expression)

List of either atoms or S-expressions

slide-8
SLIDE 8

(this (is an) (s) expression)

List of either atoms or S-expressions

slide-9
SLIDE 9

(this (is an) (s) expression)

List of either atoms or S-expressions atom

slide-10
SLIDE 10

(this (is an) (s) expression)

List of either atoms or S-expressions S-expression

slide-11
SLIDE 11

(this (is an) (s) expression)

List of either atoms or S-expressions also an S-expression

slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15

So how do we write programs in this?

slide-16
SLIDE 16

A few terms

  • LISP: The original language, grew very large over time
  • E.g., included an object system
  • Scheme: Minimal version of LISP

, partly used for teaching

  • Racket: 90s reboot of Scheme, added many new features
  • Mostly compatible w/ Scheme
slide-17
SLIDE 17

Tenants of Scheme

  • Use recursion for computation, no mutable variables
  • Basic abstraction is a list (made up of cons cells)
  • Code is data
slide-18
SLIDE 18

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x)))

slide-19
SLIDE 19

If you get stuck, use the debugger…!

slide-20
SLIDE 20

Racket is dynamically typed

slide-21
SLIDE 21

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x)))

  • Everything in parenthesis
  • Prefix notation
  • No variable assignment
  • Recursion instead of loops
  • No types
  • No return
slide-22
SLIDE 22

Here’s what most confused me…

slide-23
SLIDE 23

(define (bad_fib x) (cond [(< x 0) (raise ‘lessthanzero)] [(eq? 0 x) 1] [(eq? 1 x) 1] [else 0]) )

slide-24
SLIDE 24

Define max

  • cond
  • <
  • >
  • equal?
slide-25
SLIDE 25

Define max-of-list

  • empty?
  • first
  • rest
  • length?
slide-26
SLIDE 26

You can create functions with lambda (lambda (x) (- x)) (lambda (str) (string-ref str 0))

slide-27
SLIDE 27

(let* ([x 1] [y (+ x 1)]) (list y x)) (let ([x 1]) (+ x 1)) Rewrite this in terms of lambda!

slide-28
SLIDE 28

(let ([x 1]) (+ x 1)) Transform.. (lambda (x) (+ x 1)) 1

slide-29
SLIDE 29

(let ([x 1]) (+ x 1)) Transform.. ((lambda (x) (+ x 1)) 1)

Let is λ

slide-30
SLIDE 30

(define (f x) x) shorthand for… (define f (lambda (x) x)) Lots of other things are λ too…

slide-31
SLIDE 31

(define (f x) x) (define f (lambda (x) x)) (define (f x y) x) (define f (lambda (x y) x)) …

slide-32
SLIDE 32

(display “Hello”)

slide-33
SLIDE 33

Define acrostic

slide-34
SLIDE 34

Define hyphenate

slide-35
SLIDE 35

Using higher order functions…

slide-36
SLIDE 36

If you give me a function, I can use it (define twice (lambda (f) (lambda (x) (f (f x)))))

Challenge: figure out how I would use twice to add 2 to 2 Use Racket’s add1 function

(add1 (add1 2))

slide-37
SLIDE 37

Explain how twice works to someone next to you When listening, push the person for clarification when you get confused If you can’t figure it out, get help from someone around you

slide-38
SLIDE 38

> (map (lambda (str) (string-ref str 0)) '("ha" "ha")) '(#\h #\h)

slide-39
SLIDE 39

(map f l) takes a function f and applies f to each element of l

slide-40
SLIDE 40

[0, 1, 2]

f f f

slide-41
SLIDE 41

[0, 1, 2]

f f f

slide-42
SLIDE 42

[0, 1, 2]

f f f

[0,-1,-2]

slide-43
SLIDE 43

Tail Recursion

Tail recursion is the way you make recursion fast in functional languages Anytime I’m going to recurse more then 10k times, I use tail recursion (I also do it because it’s a fun mental exercise)

slide-44
SLIDE 44

Tail Recursion

A function is tail recursive if all recursive calls are in tail position A call is in tail position if it is the last thing to happen in a function

slide-45
SLIDE 45

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x))) The following is not tail recursive (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) The following is tail recursive

slide-46
SLIDE 46

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x))) The following is not tail recursive Explain to the person next to you why this is

slide-47
SLIDE 47
  • Swap. Explain to the person next to you why this is

(define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) The following is tail recursive

slide-48
SLIDE 48

This isn’t merely trivia!

slide-49
SLIDE 49

(define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-50
SLIDE 50

>factorial 2 1

factorial 2 1

(define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-51
SLIDE 51

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-52
SLIDE 52

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-53
SLIDE 53

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-54
SLIDE 54

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-55
SLIDE 55

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-56
SLIDE 56

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2

But wait! I don’t need the stack at all!

(define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-57
SLIDE 57

Insight: in tail recursion, the stack is just used for copying back the results

slide-58
SLIDE 58

Insight: in tail recursion, the stack is just used for copying back the results

So just forget the stack. Just give the final result to the original caller.

slide-59
SLIDE 59

Insight: in tail recursion, the stack is just used for copying back the results

So just forget the stack. Just give the final result to the original caller. This is called “tail call optimization”

slide-60
SLIDE 60

>factorial 2 1

factorial 2 1 factorial 1 2

>factorial 1 2

factorial 0 2

>factorial 0 2 (define (factorial x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) ; .. Later (factorial 2 1)

slide-61
SLIDE 61

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x))) Why couldn’t I do that with this? Talk it out with neighbor

slide-62
SLIDE 62

Tail recursion for λ and profit…

To make a function tail recursive…

  • add an extra accumulator argument
  • that tracks the result you’re building up
  • then return the result
  • might have to use more than one extra arg
  • Call function with base case as initial accumulator

This isn’t the only way to do it, just a nice trick that usually results in clean code…

slide-63
SLIDE 63

(define (factorial x) (if (equal? x 0) 1 (* (factorial (- x 1)) x))) (define (factorial-tail x acc) (if (equal? x 0) acc (factorial (- x 1) (* acc x)))) (define (factorial x) (factorial-tail 1))

slide-64
SLIDE 64

(define (max-of-list l) (cond [(= (length l) 1) 1] [(empty? l) (raise 'empty-list)] [else (max (first l) (max-of-list (rest l)) )]))

Write this as a tail-recursive function

slide-65
SLIDE 65

foldl

Like map, a higher order function operating on lists (foldl / 1 ‘(1 2 3)) = (/ 3 (/ 2 (/ 1 1))) (foldl + 0 ‘(1 2 3)) = (+ 3 (+ 2 (+ 1 0)))

slide-66
SLIDE 66

[0, 1, 2]

+

1

slide-67
SLIDE 67

[0, 1, 2]

+ +

0 1 1

slide-68
SLIDE 68

[0, 1, 2]

+ + +

0 1 3 1

slide-69
SLIDE 69

(define (concat-strings l) (foldl (lambda (next_element accumulator) (string-append next_element accumulator)) "" (reverse l)))

slide-70
SLIDE 70

Challenge: use foldl to define max-of-list

slide-71
SLIDE 71

**Challenge: define foldl