61a lecture 25
play

61A Lecture 25 Friday, October 26 Scheme is a Dialect of Lisp 2 - PowerPoint PPT Presentation

61A Lecture 25 Friday, October 26 Scheme is a Dialect of Lisp 2 Scheme is a Dialect of Lisp What are people saying about Lisp? 2 Scheme is a Dialect of Lisp What are people saying about Lisp? "The greatest single programming


  1. Special Forms A combination that is not a call expression is a special form : • If expression: (if <predicate> <consequent> <alternative>) • And and or : (and <e 1 > ... <e n >), (or <e 1 > ... <e n >) • Binding names: (define <name> <expression>) • New procedures: (define (<name> <formal parameters>) <body>) > (define pi 3.14) The name “pi” is bound to > (* pi 2) 3.14 in the global frame 6.28 4

  2. Special Forms A combination that is not a call expression is a special form : • If expression: (if <predicate> <consequent> <alternative>) • And and or : (and <e 1 > ... <e n >), (or <e 1 > ... <e n >) • Binding names: (define <name> <expression>) • New procedures: (define (<name> <formal parameters>) <body>) > (define pi 3.14) The name “pi” is bound to > (* pi 2) 3.14 in the global frame 6.28 > (define (abs x) (if (< x 0) (- x) x)) > (abs -3) 3 4

  3. Special Forms A combination that is not a call expression is a special form : • If expression: (if <predicate> <consequent> <alternative>) • And and or : (and <e 1 > ... <e n >), (or <e 1 > ... <e n >) • Binding names: (define <name> <expression>) • New procedures: (define (<name> <formal parameters>) <body>) > (define pi 3.14) The name “pi” is bound to > (* pi 2) 3.14 in the global frame 6.28 > (define (abs x) A procedure is created and (if (< x 0) bound to the name “abs” (- x) x)) > (abs -3) 3 4

  4. Special Forms A combination that is not a call expression is a special form : • If expression: (if <predicate> <consequent> <alternative>) • And and or : (and <e 1 > ... <e n >), (or <e 1 > ... <e n >) • Binding names: (define <name> <expression>) • New procedures: (define (<name> <formal parameters>) <body>) > (define pi 3.14) The name “pi” is bound to > (* pi 2) 3.14 in the global frame 6.28 > (define (abs x) A procedure is created and (if (< x 0) bound to the name “abs” (- x) x)) > (abs -3) 3 Demo 4

  5. Lambda Expressions Lambda expressions evaluate to anonymous functions. 5

  6. Lambda Expressions Lambda expressions evaluate to anonymous functions. (lambda (<formal-parameters>) <body>) 5

  7. Lambda Expressions Lambda expressions evaluate to anonymous functions. λ (lambda (<formal-parameters>) <body>) 5

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

  9. Lambda Expressions Lambda expressions evaluate to anonymous functions. λ (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: 5

  10. Lambda Expressions Lambda expressions evaluate to anonymous functions. λ (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) 5

  11. Lambda Expressions Lambda expressions evaluate to anonymous functions. λ (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 add- x -&- y -&- z 2 procedure 5

  12. Pairs and Lists 6

  13. Pairs and Lists In the late 1950s, computer scientists used confusing names. 6

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

  15. 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 • 6

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

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

  18. 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 recursive lists. 6

  19. 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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. 6

  20. 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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • 6

  21. 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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. 6

  22. 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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) 6

  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 • cdr : Procedure that returns the second element of a pair • nil : The empty list • They also used a non-obvious notation for recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) Not a well-formed list! 6

  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 • nil : The empty list • They also used a non-obvious notation for recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) Not a well-formed list! > (car x) 1 6

  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 • They also used a non-obvious notation for recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) Not a well-formed list! > (car x) 1 > (cdr x) 2 6

  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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) Not a well-formed list! > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) (1 2 3 4) 6

  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 recursive lists. A (recursive) Scheme list is a pair in which the second element is • nil or a Scheme list. Scheme lists are written as space-separated combinations. • A dotted list has an arbitrary value for the second element of the • last pair. Dotted lists may not be well-formed lists. > (define x (cons 1 2)) > x (1 . 2) Not a well-formed list! > (car x) 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) Demo (1 2 3 4) 6

  28. Symbolic Programming 7

  29. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? 7

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

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

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

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

  34. 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 > (list a b) the resulting value (1 2) 7

  35. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. 7

  36. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) 7

  37. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) 7

  38. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) > (list 'a b) 7

  39. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) > (list 'a b) (a 2) 7

  40. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (list 'a b) (a 2) 7

  41. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. 7

  42. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) 7

  43. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a 7

  44. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (list 'a b) (a 2) Quotation can also be applied to combinations to form lists. > (car '(a b c)) a > (cdr '(a b c)) 7

  45. 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 > (list a b) the resulting value (1 2) Quotation is used to refer to symbols directly in Lisp. > (list 'a 'b) (a b) Symbols are now values > (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) 7

  46. Scheme Lists and Quotation 8

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

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

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

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

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

  52. 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) 1 2 3 8

  53. 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) 1 2 3 (1 2 . 3) 8

  54. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) 8

  55. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) 1 2 8

  56. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 8

  57. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) 8

  58. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 8

  59. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 1 2 3 nil 8

  60. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 1 2 3 nil (1 2 3) 8

  61. 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) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 1 2 3 nil (1 2 3) What is the printed result of evaluating this expression? 8

  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))) 3 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 1 2 3 nil (1 2 3) What is the printed result of evaluating this expression? > (cdr '((1 2) . (3 4 . (5)))) 8

  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 However, dots appear in the output only of ill-formed lists. > '(1 2 . 3) 1 2 3 (1 2 . 3) > '(1 2 . (3 4)) nil 1 2 3 4 (1 2 3 4) > '(1 2 3 . nil) 1 2 3 nil (1 2 3) What is the printed result of evaluating this expression? > (cdr '((1 2) . (3 4 . (5)))) (3 4 5) 8

  64. Coercing a Sorted List to a Binary Search Tree 9

  65. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 9

  66. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] 9

  67. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] Recursively coerce the left part 9

  68. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] Recursively coerce the left part 9

  69. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 2 1 3 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] Recursively coerce the left part 9

  70. Coercing a Sorted List to a Binary Search Tree 1 2 3 4 5 6 7 2 1 3 Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] Recursively coerce the left part 9

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