cs 61a discussion 9
play

CS 61A Discussion 9 Scheme Albert Xu Attendance: - PowerPoint PPT Presentation

CS 61A Discussion 9 Scheme Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Major Differences Scheme has a fundamental philosophical di ff erence from languages like Python. Major Differences


  1. 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)

  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)

  3. 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)

  4. 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)

  5. 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)

  6. 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)

  7. 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)

  8. 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)

  9. 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 …

  10. Special Forms unlike call expressions…

  11. 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!

  12. 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!

  13. 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

  14. 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!

  15. 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)

  16. 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.

  17. 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.

  18. 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

  19. 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

  20. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy!

  21. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1

  22. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1

  23. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

  24. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t

  25. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0

  26. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0

  27. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f

  28. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f

  29. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil

  30. if, booleans Remember! The only falsy value in Scheme is #f, everything else is truthy! 1 #t 0 #f nil

  31. 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>)

  32. 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> .

  33. 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> .

  34. 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> .

  35. 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> .

  36. 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> .

  37. 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> .

  38. 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> .

  39. short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f)

  40. short circuiting Syntax: (and/or <args>) Short circuiting works the same in Scheme as it does in Python! >>> (or 1 #f) 1

  41. 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)

  42. 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

  43. 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)

  44. 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

  45. lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2

  46. lambdas are here to stay Convert this Python code to Scheme: square = lambda x: x**2 (define square (lambda (x) (* x x)))

  47. 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!

  48. 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))

  49. Lists in Scheme remember linked lists?

  50. 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

  51. 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

  52. 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

  53. 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

  54. 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)

  55. Equality in scheme = can only be used for comparing numbers. (= 2 2) (= ‘(1) ‘(1))

Recommend


More recommend