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

equality-distinctions.lsp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ;; the most common equality predicates are eq, eql, equal and equalp
  15. ;; eq is similar to comparing c pointers
  16. (define-test test-eq
  17. "(eq x y) is true if and only if x and y are the same identical object
  18. eq is like comparing pointers in c. If the values are EQ, any non-nil
  19. value may be returned."
  20. (true-or-false? ___ (eq 'a 'a))
  21. (true-or-false? ___ (eq 3 3.0))
  22. (true-or-false? ___ (eq '(1 2) '(1 2)))
  23. (true-or-false? ___ (eq "Foo" "Foo"))
  24. (true-or-false? ___ (eq "Foo" (copy-seq "Foo")))
  25. (true-or-false? ___ (eq "FOO" "Foo")))
  26. (define-test test-eql
  27. "(eql x y) is true if (eq x y)
  28. also it is true if x and y are numeric of the same type
  29. and represent the same number.
  30. (eql x y) also if x and y are the same characters."
  31. (true-or-false? ___ (eql 'a 'a))
  32. (true-or-false? ___ (eql 3 3))
  33. (true-or-false? ___ (eql 3 3.0))
  34. (true-or-false? ___ (eql '(1 2) '(1 2)))
  35. (true-or-false? ___ (eql '(:a . :b) '(:a . :b)))
  36. (true-or-false? ___ (eql #\S #\S))
  37. (true-or-false? ___ (eql "Foo" "Foo"))
  38. (true-or-false? ___ (eql "Foo" (copy-seq "Foo")))
  39. (true-or-false? ___ (eql "FOO" "Foo")))
  40. (define-test test-equal
  41. "(equal x y) is true if (eql x y), or
  42. x and y are lists with equal elements, or
  43. x and y character or bit arrays with equal elements"
  44. (true-or-false? ___ (equal 'a 'a))
  45. (true-or-false? ___ (equal 3 3))
  46. (true-or-false? ___ (equal 3 3.0))
  47. (true-or-false? ___ (equal '(1 2) '(1 2)))
  48. (true-or-false? ___ (equal '(:a . :b) '(:a . :b)))
  49. (true-or-false? ___ (equal '(:a . :b) '(:a . :doesnt-match)))
  50. (true-or-false? ___ (equal #\S #\S))
  51. (true-or-false? ___ (equal "Foo" "Foo"))
  52. (true-or-false? ___ (equal "Foo" (copy-seq "Foo")))
  53. (true-or-false? ___ (equal "FOO" "Foo")))
  54. (define-test test-equalp
  55. "(equalp x y) if (equal x y) or
  56. if x and y are strings with the same characters (case independent).
  57. if x and y are arrays with the same dimensions and equal elements
  58. if x and y are numeric of different types but one may be upgraded to
  59. the other type without loss and still exhibit equality."
  60. (true-or-false? ___ (equalp 'a 'a))
  61. (true-or-false? ___ (equalp 3 3))
  62. (true-or-false? ___ (equalp 3 3.0))
  63. (true-or-false? ___ (equalp '(1 2) '(1 2)))
  64. (true-or-false? ___ (equalp '(:a . :b) '(:a . :b)))
  65. (true-or-false? ___ (equalp '(:a . :b) '(:a . :doesnt-match)))
  66. (true-or-false? ___ (equalp #\S #\S))
  67. (true-or-false? ___ (equalp "Foo" "Foo"))
  68. (true-or-false? ___ (equalp "Foo" (copy-seq "Foo")))
  69. (true-or-false? ___ (equalp "FOO" "Foo")))
  70. (define-test test-numeric-equal
  71. "(= x y) is only for numerics
  72. and can take multiple arguments
  73. if x or y is not numeric there will be a compiler error."
  74. (true-or-false? ___ (= 99.0 99 99.000))
  75. (true-or-false? ___ (= 0 1 -1))
  76. (true-or-false? ___ (= (/ 2 3) (/ 6 9) (/ 86 129))))