Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: What are the rules of evaluating call expressions? 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating call expressions? 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f (lambda (x) (+ x 1)) 1) Evaluate the operator 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f (lambda (x) (+ x 1)) 1) Evaluate the operator >>> (f 1) 2) Evaluate the operands 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f (lambda (x) (+ x 1)) 1) Evaluate the operator >>> (f 1) 2) Evaluate the operands 2 3) Apply the result of (1) to the result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f (lambda (x) (+ x 1)) 1) Evaluate the operator >>> (f 1) 2) Evaluate the operands 2 3) Apply the result of (1) to the >>> (f 1 2) result of (2)
Call Expressions, again Syntax: (<operator> <op-1> <op-2> … <op-n>) Reminder: >>> (define (f x) (+ x 1)) What are the rules of evaluating f call expressions? >>> f (lambda (x) (+ x 1)) 1) Evaluate the operator >>> (f 1) 2) Evaluate the operands 2 3) Apply the result of (1) to the >>> (f 1 2) result of (2) Error: Too many arguments …
Special Forms unlike call expressions…
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do!
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all!
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form!
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form! (define x 1)
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form! (define x 1) Short-circuiting means that we can’t evaluate an operand until we know the truth value of the previous operand.
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form! (define x 1) Short-circuiting means that we can’t evaluate an operand until we know the (or 2 (/ 1 0) truth value of the previous operand.
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form! (define x 1) Short-circuiting means that we can’t evaluate an operand until we know the (or 2 (/ 1 0) truth value of the previous operand. if expressions should only evaluate the <if-true> or <if-false> operand
What is a Special Form? There’s an issue with call expressions - it doesn’t work for all of the things we want our Scheme language to do! Special forms don’t follow the evaluation rules we just talked about! Usually this means we evaluate the operands in a di ff erent order, or don’t evaluate them at all! Examples define is a special form! (define x 1) Short-circuiting means that we can’t evaluate an operand until we know the (or 2 (/ 1 0) truth value of the previous operand. if expressions should only evaluate the (if #t (+ 1 2) (- 2 1)) <if-true> or <if-false> operand
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy!
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> <if-true> <if-false>)
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> <if-true> <if-false>) • If <condition> is truthy, eval and return <if-true> . Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) • If <condition> is truthy, eval and return <if-true> . Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) x • If <condition> is truthy, eval and return <if-true> . Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) x • If <condition> is truthy, >>> (if x (+ 1 1) (/ 1 0)) eval and return <if-true> . Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) x • If <condition> is truthy, >>> (if x (+ 1 1) (/ 1 0)) 2 eval and return <if-true> . Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) x • If <condition> is truthy, >>> (if x (+ 1 1) (/ 1 0)) 2 eval and return <if-true> . >>> (if 0 (/ 1 0) (+ 1 1)) Otherwise eval and return <if-false> .
if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil if special form • Syntax : (if <condition> >>> (define x #t) <if-true> <if-false>) x • If <condition> is truthy, >>> (if x (+ 1 1) (/ 1 0)) 2 eval and return <if-true> . >>> (if 0 (/ 1 0) (+ 1 1)) Otherwise eval and return Error: Cannot divide by 0 <if-false> .
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f)
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3)
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f >>> (and 2 3)
short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1 >>> (and #f 2 3) #f >>> (and 2 3) 3
lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2
lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2 (define square (lambda (x) (* x x)))
lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2 (define square (lambda (x) (* x x))) All functions in Scheme are lambda functions!
lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2 (define square (lambda (x) (* x x))) All functions in Scheme are lambda functions! >>> (define (f x) (+ x 1)) f >>> f (lambda (x) (+ x 1))
Lists in Scheme remember linked lists?
List Basics • Scheme lists are like Python’s Pair. Both are linked lists. • Attributes • first - the current list element • rest - the rest of the elements
List Basics • Scheme lists are like Python’s Pair. Both are linked lists. • Attributes • first - the current list element • rest - the rest of the elements • Accessing List Values • car - get the first element of a list • cdr - get the rest of a list
List Basics • Scheme lists are like Python’s Pair. Both are linked lists. • Attributes • first - the current list element • rest - the rest of the elements • Accessing List Values • car - get the first element of a list • cdr - get the rest of a list Pair(1, Pair(4, Pair(9, nil))) first rest 1 4 9
List Basics • Scheme lists are like Python’s Pair. Both are linked lists. • Attributes • first - the current list element • rest - the rest of the elements • Accessing List Values • car - get the first element of a list • cdr - get the rest of a list Pair(1, Pair(4, Pair(9, nil))) (cons 1 (cons 4 (cons 9 nil))) first rest 1 4 9
Equality in scheme = can only be used for comparing numbers. (= 2 2) (= ‘(1) ‘(1)) eq? behaves like == in Python for comparing two non-pairs (numbers, booleans, etc.). Otherwise, eq? behaves like is in Python (define lst ‘(1)) (eq? 2 2) (eq? #t #t) (eq? ‘(1) ‘(1)) (eq? lst lst)
Equality in scheme = can only be used for comparing numbers. (= 2 2) (= ‘(1) ‘(1))
Recommend
More recommend