Fork of https://github.com/google/lisp-koans so that I could go through them. THIS CONTAINS ANSWERS.

special-forms.lsp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;; Copyright 2013 Google Inc.
  2. ;;
  3. ;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;; you may not use this file except in compliance with the License.
  5. ;; You may obtain a copy of the License at
  6. ;;
  7. ;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;
  9. ;; Unless required by applicable law or agreed to in writing, software
  10. ;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;; See the License for the specific language governing permissions and
  13. ;; limitations under the License.
  14. ; Special forms are evaluatable lisp forms (lists) which are
  15. ; neither functions nor macros. Here is an introduction to a
  16. ; few of them.
  17. ; based on http://psg.com/~dlamkins/sl/chapter03-03.html
  18. (defvar my-name)
  19. (defvar my-clones-name)
  20. (defvar a)
  21. (defvar b)
  22. (defvar c 0)
  23. (define-test test-setf
  24. "setf is used to assign values to symbols. These symbols may refer to
  25. variables with lexical or dynamic scope."
  26. (setf my-name "David")
  27. (assert-equal my-name "David")
  28. " In SBCL, if the symbol isn't defined as a variable, via a top-level defvar
  29. or let statement, the setf call may result in a warning."
  30. (setf my-clones-name my-name)
  31. (assert-equal "David" my-clones-name)
  32. (setf a 5)
  33. (setf b 10)
  34. (setf c 50)
  35. (assert-equal 50 c))
  36. (define-test test-let
  37. "The let form establishes a lexical extent, within which explicit symbols
  38. may be bound to values. The binding only extends over the extent of the
  39. lexical form. After which, the previous value, if it exists, is visible again."
  40. (setf a 10)
  41. (setf b 20)
  42. (assert-equal a 10)
  43. (assert-equal b 20)
  44. (let ((a 1111)
  45. (b 2222))
  46. (assert-equal a 1111)
  47. (assert-equal b 2222))
  48. (assert-equal a 10)
  49. (assert-equal b 20))
  50. (define-test test-let-default-value
  51. "let vars have a default value"
  52. (let ((x))
  53. (assert-equal nil x)))
  54. (define-test test-let-bindings-are-parallel
  55. "When defining the bindings in the let form, later bindings may not depend
  56. on earlier ones"
  57. (setf a 100)
  58. (let ((a 5)
  59. (b (* 10 a)))
  60. (assert-equal b 1000)))
  61. (define-test test-let*-bindings-are-series
  62. "let* is like let, but successive bindings may use values of previous ones"
  63. (setf a 100)
  64. (let* ((a 5)
  65. (b (* 10 a)))
  66. (assert-equal b 50))
  67. (assert-equal a 100))
  68. (define-test write-your-own-let-statement
  69. "fix the let statement to get the tests to pass"
  70. (setf a 100)
  71. (setf b 23)
  72. (setf c 456)
  73. (let ((a a)
  74. (b (* a 2))
  75. (c "Jellyfish"))
  76. (assert-equal a 100)
  77. (assert-equal b 200)
  78. (assert-equal c "Jellyfish"))
  79. (let* ((a 121)
  80. (b 200)
  81. (c (+ a (/ b a))))
  82. (assert-equal a 121)
  83. (assert-equal b 200)
  84. (assert-equal c (+ a (/ b a)))))
  85. (define-test test-cond
  86. "the cond form is like the c switch statement"
  87. (setf a 4)
  88. (setf c
  89. (cond ((> a 0) :positive)
  90. ((< a 0) :negative)
  91. (t :zero)))
  92. (assert-equal :positive c))
  93. (defun cartoon-dads (input)
  94. " you should be able to complete this cond statement"
  95. (cond ((equal input :this-one-doesnt-happen) :fancy-cat)
  96. ((equal input :bart) :homer)
  97. ((equal input :stewie) :peter)
  98. ((equal input :stan) :randy)
  99. (t :unknown)))
  100. (define-test test-your-own-cond-statement
  101. "fix this by completing the 'cartoon-dads' function above"
  102. (assert-equal (cartoon-dads :bart) :homer)
  103. (assert-equal (cartoon-dads :stewie) :peter)
  104. (assert-equal (cartoon-dads :stan) :randy)
  105. (assert-equal (cartoon-dads :space-ghost) :unknown))