scheme announcements scheme scheme is a dialect of lisp
play

Scheme Announcements Scheme Scheme is a Dialect of Lisp 4 Scheme - PowerPoint PPT Presentation

Scheme Announcements Scheme Scheme is a Dialect of Lisp 4 Scheme is a Dialect of Lisp What are people saying about Lisp? 4 Scheme is a Dialect of Lisp What are people saying about Lisp? "If you don't know Lisp, you don't know what


  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 > (define pi 3.14) The symbol “pi” is bound to 3.14 in the > (* pi 2) global frame 6.28 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 • 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

  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 • 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

  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 > (define (abs x) A procedure is created and bound to the (if (< x 0) symbol “abs” (- x) x)) > (abs -3) 3 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) A procedure is created and bound to the (if (< x 0) symbol “abs” (- 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 (Demo) 7

  7. Scheme Interpreters (Demo)

  8. Lambda Expressions

  9. Lambda Expressions Lambda expressions evaluate to anonymous procedures 10

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

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

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

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

  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))) An operator can be a call expression too: ((lambda (x y z) (+ x y (square z))) 1 2 3) 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: ((lambda (x y z) (+ x y (square z))) 1 2 3) Evaluates to the x+y+z 2 procedure 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) 12 Evaluates to the x+y+z 2 procedure 10

  17. Sierpinski's Triangle (Demo)

  18. More Special Forms

  19. Cond & Begin 13

  20. Cond & Begin The cond special form that behaves like if-elif-else statements in Python 13

  21. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: print('big') elif x > 5: print('medium') else : print('small') 13

  22. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: print('big') ( cond ((> x 10) (print 'big)) elif x > 5: ((> x 5) (print 'medium)) print('medium') ( else (print 'small))) else : print('small') 13

  23. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ( else 'small)) else : print('small') 13

  24. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: (print print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ) ( else 'small)) else : print('small') 13

  25. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: (print print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ) ( else 'small)) else : print('small') The begin special form combines multiple expressions into one expression 13

  26. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: (print print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ) ( else 'small)) else : print('small') The begin special form combines multiple expressions into one expression if x > 10: print('big') print('guy') else : print('small') print('fry') 13

  27. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: (print print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ) ( else 'small)) else : print('small') The begin special form combines multiple expressions into one expression ( cond ((> x 10) ( begin (print 'big) (print 'guy))) if x > 10: ( else ( begin (print 'small) (print 'fry)))) print('big') print('guy') else : print('small') print('fry') 13

  28. Cond & Begin The cond special form that behaves like if-elif-else statements in Python if x > 10: (print print('big') ( cond ((> x 10) (print 'big)) ( cond ((> x 10) 'big) elif x > 5: ((> x 5) (print 'medium)) ((> x 5) 'medium) print('medium') ( else (print 'small))) ) ( else 'small)) else : print('small') The begin special form combines multiple expressions into one expression ( cond ((> x 10) ( begin (print 'big) (print 'guy))) if x > 10: ( else ( begin (print 'small) (print 'fry)))) print('big') print('guy') else : ( if (> x 10) ( begin print('small') (print 'big) print('fry') (print 'guy)) ( begin (print 'small) (print 'fry))) 13

  29. Let Expressions The let special form binds symbols to values temporarily; just for one expression 14

  30. Let Expressions The let special form binds symbols to values temporarily; just for one expression a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b) 14

  31. Let Expressions The let special form binds symbols to values temporarily; just for one expression a = 3 b = 2 + 2 c = math.sqrt(a * a + b * b) a and b are still bound down here 14

  32. Let Expressions The let special form binds symbols to values temporarily; just for one expression a = 3 ( define c ( let ((a 3) b = 2 + 2 (b (+ 2 2))) c = math.sqrt(a * a + b * b) (sqrt (+ (* a a) (* b b))))) a and b are still bound down here 14

  33. Let Expressions The let special form binds symbols to values temporarily; just for one expression a = 3 ( define c ( let ((a 3) b = 2 + 2 (b (+ 2 2))) c = math.sqrt(a * a + b * b) (sqrt (+ (* a a) (* b b))))) a and b are still bound down here a and b are not bound down here 14

  34. Lists

  35. Scheme Lists

  36. Scheme Lists In the late 1950s, computer scientists used confusing names

  37. Scheme Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a linked list

  38. Scheme Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list

  39. Scheme Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list • cdr : Procedure that returns the rest of a list

  40. Scheme Lists In the late 1950s, computer scientists used confusing names • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list • cdr : Procedure that returns the rest of a list • nil : The empty list

  41. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list • cdr : Procedure that returns the rest of a list • nil : The empty list

  42. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list

  43. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces

  44. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces (cons 2 nil) 2

  45. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1

  46. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2)

  47. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil))

  48. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x

  49. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2)

  50. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x)

  51. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1

  52. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x)

  53. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2)

  54. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil))))

  55. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) 1 2 3 4

  56. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) 1 2 3 4 (1 2 3 4)

  57. Scheme Lists In the late 1950s, computer scientists used confusing names (cons 2 nil) 2 nil • cons : Two-argument procedure that creates a linked list • car : Procedure that returns the first element of a list 2 • cdr : Procedure that returns the rest of a list • nil : The empty list Important! Scheme lists are written in parentheses with elements separated by spaces > (cons 1 ) (cons 2 nil) 2 1 (1 2) > (define x (cons 1 (cons 2 nil)) > x (1 2) > (car x) 1 > (cdr x) (2) > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) 1 2 3 4 (1 2 3 4) (Demo)

  58. Symbolic Programming

  59. Symbolic Programming 18

  60. Symbolic Programming Symbols normally refer to values; how do we refer to symbols? 18

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

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

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

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

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

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

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

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

Recommend


More recommend