61a lecture 24
play

61A Lecture 24 Monday, March 30 Announcements 2 Announcements - PowerPoint PPT Presentation

61A Lecture 24 Monday, March 30 Announcements 2 Announcements Homework 7 due Wednesday 4/8 @ 11:59pm 2 Announcements Homework 7 due Wednesday 4/8 @ 11:59pm Quiz 3 released Tuesday 4/7, due Thursday 4/9 @ 11:59pm 2 Announcements


  1. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative 7

  2. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative > (define pi 3.14) > (* pi 2) 6.28 7

  3. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 7

  4. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative • New procedures: (define (<symbol> <formal parameters>) <body>) > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 7

  5. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative • New procedures: (define (<symbol> <formal parameters>) <body>) > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 > (define (abs x) (if (< x 0) (- x) x)) > (abs -3) 3 7

  6. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative • New procedures: (define (<symbol> <formal parameters>) <body>) > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 > (define (abs x) A procedure is created and bound to the (if (< x 0) symbol “abs” (- x) x)) > (abs -3) 3 7

  7. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative • New procedures: (define (<symbol> <formal parameters>) <body>) > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 > (define (abs x) A procedure is created and bound to the (if (< x 0) symbol “abs” (- x) x)) > (abs -3) 3 7

  8. Special Forms A combination that is not a call expression is a special form: Evaluation: • if expression: (if <predicate> <consequent> <alternative>) (1) Evaluate the predicate expression • and and or : (and <e1> ... <en>), (or <e1> ... <en>) (2) Evaluate either • Binding symbols: (define <symbol> <expression>) the consequent or alternative • New procedures: (define (<symbol> <formal parameters>) <body>) > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 > (define (abs x) A procedure is created and bound to the (if (< x 0) symbol “abs” (- x) x)) > (abs -3) 3 (Demo) 7

  9. Scheme Interpreters (Demo)

  10. Lambda Expressions

  11. Lambda Expressions Lambda expressions evaluate to anonymous procedures 10

  12. Lambda Expressions Lambda expressions evaluate to anonymous procedures (lambda (<formal-parameters>) <body>) 10

  13. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) 10

  14. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) 10

  15. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: 10

  16. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3) 10

  17. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3) Evaluates to the 
 x+y+z 2 procedure 10

  18. Lambda Expressions Lambda expressions evaluate to anonymous procedures λ (lambda (<formal-parameters>) <body>) Two equivalent expressions: (define (plus4 x) (+ x 4)) (define plus4 (lambda (x) (+ x 4))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3) 12 Evaluates to the 
 x+y+z 2 procedure 10

  19. Pairs and Lists

  20. Pairs and Lists 12

  21. Pairs and Lists In the late 1950s, computer scientists used confusing names 12

  22. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair 12

  23. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair 12

  24. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair 12

  25. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list 12

  26. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists 12

  27. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. 12

  28. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces 12

  29. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! 12

  30. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) 12

  31. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x 12

  32. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) 12

  33. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 12

  34. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 12

  35. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 12

  36. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 12

  37. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) 12

  38. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4) 12

  39. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4) 12

  40. Pairs and Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a pair • car : Procedure that returns the first element of a pair • cdr : Procedure that returns the second element of a pair • nil : The empty list They also used a non-obvious notation for linked lists • A (linked) list in Scheme is a pair in which the second element is nil or a Scheme list. • Important! Scheme lists are written in parentheses separated by spaces • A dotted list has any value for the second element of the last pair; maybe not a list! > (define x (cons 1 2)) > x (1 . 2) > (car x) Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4) (Demo) 12

  41. Symbolic Programming

  42. Symbolic Programming 14

  43. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? 14

  44. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) 14

  45. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) 14

  46. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) 14

  47. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) > (list a b) (1 2) 14

  48. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) 14

  49. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. 14

  50. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) 14

  51. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) 14

  52. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) > (list 'a b) 14

  53. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) > (list 'a b) (a 2) 14

  54. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) 14

  55. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. 14

  56. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) 14

  57. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a 14

  58. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a > (cdr '(a b c)) 14

  59. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? > (define a 1) > (define b 2) No sign of “a” and “b” in the > (list a b) resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) Symbols are now values (a b) > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a > (cdr '(a b c)) (b c) 14

  60. Scheme Lists and Quotation 15

  61. Scheme Lists and Quotation Dots can be used in a quoted list to specify the second element of the final pair. 15

  62. Scheme Lists and Quotation Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 15

  63. Scheme Lists and Quotation Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 15

  64. Scheme Lists and Quotation Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. 15

  65. Scheme Lists and Quotation Dots can be used in a quoted list to specify the second element of the final pair. > (cdr (cdr '(1 2 . 3))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend