excluded-sem8-simplecontext.story 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Some simple context-aware operations
  2. Description:
  3. As in any language, lisp interpreters allow to
  4. store information in "variables".
  5. A variable is an identifier, which is associated to a lisp
  6. expression using the operator "define".
  7. To update the value associated with a variable,
  8. a specific "set!" operator must be used.
  9. The scope of those variables is by default global, i.e.
  10. they can be accessed in any lisp expression after being
  11. defined.
  12. A specific case is the one of function (lambda expression) parameters.
  13. Those variables are not defined explicitly.
  14. Their scope is limited to the body of the lambda expression.
  15. Narrative:
  16. In order to reuse information,
  17. as a casual lisp user
  18. I want to associate expressions to identifiers using the keyword define.
  19. Scenario: simple definitions
  20. Given a lisp interpreter
  21. When the expression entered is (define a 5)
  22. Then the result should be 5
  23. When the expression entered is (define b 10)
  24. Then the result should be 10
  25. When the expression entered is <expression>
  26. Then the result should be <result>
  27. Examples:
  28. |expression |result|
  29. |(+ a b) | 15 |
  30. |(- a b) | -5 |
  31. |(* a b) | 50 |
  32. |(* 1 a 2 b) | 100 |
  33. |(/ b a) | 2 |
  34. |(max a b) | 10 |
  35. |(min a b) | 5 |
  36. |(cons a (cons b ()))| (5 10) |
  37. Scenario: overriding definitions
  38. Given a lisp interpreter
  39. When the expression entered is (define a 5)
  40. Then the result should be 5
  41. When the expression entered is (define b 10)
  42. Then the result should be 10
  43. When the expression entered is (+ a b)
  44. Then the result should be 15
  45. When the expression entered is (define b 30)
  46. Then the result should be 30
  47. When the expression entered is (+ a b)
  48. Then the result should be 35
  49. When the expression entered is (define a 20)
  50. Then the result should be 20
  51. When the expression entered is (+ a b)
  52. Then the result should be 50
  53. Scenario: some students are picky about testing
  54. Given a lisp interpreter
  55. When the expression entered is <expression>
  56. Then the result should display the error message <result>
  57. Examples:
  58. |expression|result|
  59. |(define 12 20)|12 is not a valid identifier|
  60. |(define + 20)|+ is not a valid identifier|
  61. |(define - 20)|- is not a valid identifier|
  62. |(define * 20)|* is not a valid identifier|
  63. |(define / 20)|/ is not a valid identifier|
  64. |(define or 20)|or is not a valid identifier|
  65. |(define not 20)|not is not a valid identifier|
  66. |(define and 20)|and is not a valid identifier|
  67. |(define lambda 20)|lambda is not a valid identifier|
  68. |(define define 20)|define is not a valid identifier|
  69. |(define set! 20)|set! is not a valid identifier|
  70. |(define cons 20)|cons is not a valid identifier|
  71. Scenario: modifying the value of an identifier using set!
  72. Given a lisp interpreter
  73. When the expression entered is (define a 50)
  74. Then the result should be 50
  75. When the expression entered is (set! a 30)
  76. Then the result should be 30
  77. When the expression entered is (set! b 20)
  78. Then the result should display the error message b is undefined
  79. Scenario: defining a function (a lambda expression) with one parameter
  80. Given a lisp interpreter
  81. When the expression entered is (define surprise (lambda (x) (* x x)))
  82. Then the result should be lambda (x) (* x x)
  83. When the expression entered is (surprise 10)
  84. Then the result should be 100
  85. Scenario: defining a function (a lambda expression) with two parameters
  86. Given a lisp interpreter
  87. When the expression entered is (define surprise (lambda (x y) (* x y)))
  88. Then the result should be lambda (x y) (* x y)
  89. When the expression entered is (surprise 10 20)
  90. Then the result should be 200
  91. Scenario: a function has its own context
  92. Given a lisp interpreter
  93. When the expression entered is (define x 10)
  94. Then the result should be 10
  95. When the expression entered is x
  96. Then the result should be 10
  97. When the expression entered is (define surprise (lambda (x) (* x x)))
  98. Then the result should be lambda (x) (* x x)
  99. When the expression entered is (surprise 15)
  100. Then the result should be 225
  101. When the expression entered is x
  102. Then the result should be 10
  103. Scenario: identifier is not defined
  104. Given a lisp interpreter
  105. When the expression entered is x
  106. Then the result should display the error message x is undefined
  107. When the expression entered is (surprise 15)
  108. Then the result should display the error message surprise is undefined
  109. Scenario: first group inspiration
  110. Given a lisp interpreter
  111. When the expression entered is (define n 10)
  112. Then the result should be 10
  113. When the expression entered is (define f (lambda (x) (* n x)))
  114. Then the result should be lambda (x) (* n x)
  115. When the expression entered is (f 4)
  116. Then the result should be 40
  117. When the expression entered is (set! n 100)
  118. Then the result should be 100
  119. When the expression entered is (f 4)
  120. Then the result should be 400