AAQZ4 and moving to Unamed Functions

Table of Contents

1. Nested Functions

def compose(f, g):
    "return the function f º g"
    return lambda x : f(g(x))

def double (x):
    return 2 + x
def add_five(x):
    return x + 5

print(compose(double, add_five) (10)) => 30
print(compose(add_five, double) (10)) => 25

in racket…

(define (compose f g)
     (lambda (x) (f (g x))))

2. AAQZ4

  • we want functiosn as values
    • previously interp : ExprC -> Real
    • interp : ExprC -> Value

where Value = (U NumV CloV BoolV StrV …)

consider {+ 1 2} or maybe {+ 1 “hello”}

  • interpreter see and says ERROR!
<expr>::= <num
        | <id>
        | {<expr> <expr> ...}
        | {if <expr> <expr> <expr>}
        | <str>
        | {(<id> ...) => <expr>} <- anyonmyous functions


Now there are no named functions so complete program is just one expression.

Reduction Step Example

{ {(h) => {h 8}} {(x) => {+ x 1}} }

= { {(x) => {+ x 1}} 8 }

= {+ 8 1}

= 9

Another way to give a function a name

create a function named something and pass the function into the name.

{{(compose) => {}} <compose-def>}

Date: 2024-10-18 Fri 00:00

Author: Anthony Rossi

Created: 2024-10-20 Sun 18:15