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

nil-false-empty.lsp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. (define-test test-t-and-nil-are-opposites
  15. "not is a function which returns the boolean opposite of its argument"
  16. (true-or-false? t (not nil))
  17. (true-or-false? nil (not t)))
  18. (define-test test-nil-and-empty-list-are-the-same-thing
  19. (true-or-false? nil ())
  20. (true-or-false? t (not ())))
  21. (define-test test-lots-of-things-are-true
  22. " every value, other than nil, is boolean true"
  23. (true-or-false? t 5)
  24. (true-or-false? nil (not 5))
  25. (true-or-false? t "A String")
  26. "only nil is nil. Everything else is effectively true."
  27. "the empty string"
  28. (true-or-false? t "")
  29. "a list containing a nil"
  30. (true-or-false? t '(nil))
  31. "an array with no elements"
  32. (true-or-false? t (make-array '(0)))
  33. "the number zero"
  34. (true-or-false? t 0))
  35. (define-test test-and
  36. "and can take multiple arguments"
  37. (true-or-false? t (and t t t t t))
  38. (true-or-false? nil (and t t nil t t))
  39. "if no nils, and returns the last value"
  40. (assert-equal 5 (and t t t t t 5)))
  41. (define-test test-or
  42. "or can also take multiple arguments"
  43. (true-or-false? t (or nil nil nil t nil))
  44. "or returns the first non nil value, or nil if there are none."
  45. (assert-equal nil (or nil nil nil))
  46. (assert-equal 1 (or 1 2 3 4 5)))