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

type-checking.lsp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ;; Common lisp types have hierarchy. Any object may belong a family of types.
  15. ;; The top level type, which includes everything else, is 't'
  16. (define-test test-check-some-common-types
  17. (true-or-false? ___ (typep "hello" 'string))
  18. (true-or-false? ___ (typep "hello" 'array))
  19. (true-or-false? ___ (typep "hello" 'list))
  20. (true-or-false? ___ (typep "hello" '(simple-array character (5))))
  21. (true-or-false? ___ (typep '(1 2 3) 'list))
  22. (true-or-false? ___ (typep 99 'integer))
  23. (true-or-false? ___ (typep nil 'NULL))
  24. (true-or-false? ___ (typep 22/7 'ratio))
  25. (true-or-false? ___ (typep 4.0 'float))
  26. (true-or-false? ___ (typep #\a 'character))
  27. (true-or-false? ___ (typep #'length 'function)))
  28. (define-test test-get-type-with-type-of
  29. (assert-equal ____ (type-of ()))
  30. (assert-equal ____ (type-of 4/6)))
  31. (define-test test-type-sets-may-overlap
  32. (true-or-false? ___ (typep () 'list))
  33. (true-or-false? ___ (typep () 'atom))
  34. (true-or-false? ___ (typep () 'NULL))
  35. (true-or-false? ___ (typep () t)))
  36. (define-test test-integers-can-get-really-big
  37. (true-or-false? ____ (typep 12345678901234567890123456789012 'integer))
  38. ;; Integers are either fixnum or bignum.
  39. ;; The boundary between fixnum and bignum is given by the constant:
  40. ;; most-positive-fixnum
  41. (assert-true (typep 1234567890123456789 'fixnum))
  42. (assert-true (typep 12345678901234567890 'bignum))
  43. (true-or-false? ___ (typep most-positive-fixnum 'fixnum))
  44. (true-or-false? ___ (typep (+ 1 most-positive-fixnum) 'fixnum)))
  45. (define-test test-lisp-type-system-is-hierarchy
  46. (assert-true (typep 1 'bit))
  47. (assert-true (typep 1 'integer))
  48. (assert-true (typep 2 'integer))
  49. (true-or-false? ____ (subtypep 'bit 'integer))
  50. (true-or-false? ____ (subtypep (type-of 1) (type-of 2)))
  51. (true-or-false? ____ (subtypep (type-of 2) (type-of 1))))
  52. (define-test test-some-types-are-lists
  53. (assert-true(typep (make-array 0 :element-type 'integer) '(SIMPLE-VECTOR 0)))
  54. (true-or-false? ____ (typep (make-array '(3 3) :element-type 'integer) '(SIMPLE-ARRAY T (3 3)))))
  55. (define-test test-type-specifier-lists-also-have-hierarchy
  56. (true-or-false? ____ (subtypep '(SIMPLE-ARRAY T (3 3)) '(SIMPLE-ARRAY T *)))
  57. (true-or-false? ____ (subtypep '(vector double-float 100) '(vector * 100)))
  58. (true-or-false? ____ (subtypep '(vector double-float 100) '(vector double-float *)))
  59. (true-or-false? ____ (subtypep '(vector double-float 100) '(vector * *)))
  60. (true-or-false? ____ (subtypep '(vector double-float 100) '(array number *)))
  61. (true-or-false? ____ (subtypep '(vector double-float 100) t)))
  62. (define-test test-type-coersion
  63. (assert-true (typep 0 'integer))
  64. (true-or-false? ___ (typep 0 'short-float))
  65. (true-or-false? ___ (subtypep 'integer 'short-float))
  66. (true-or-false? ___ (subtypep 'short-float 'integer))
  67. (true-or-false? ___ (typep (coerce 0 'short-float) 'short-float)))
  68. (define-test test-atoms-are-anything-thats-not-a-cons
  69. (true-or-false? ___ (atom 4))
  70. (true-or-false? ___ (atom '(1 2 3 4)))
  71. (true-or-false? ___ (atom 'some-unbound-name))
  72. (assert-true (typep (make-array '(4 4)) '(SIMPLE-ARRAY * *)))
  73. (true-or-false? ___ (atom (make-array '(4 4)))))
  74. (define-test test-functionp
  75. "the functionp predicate is true iff the argument is a function"
  76. (assert-true (functionp (lambda (a b c) (+ a b c))))
  77. (true-or-false? ___ (functionp #'make-array))
  78. (true-or-false? ___ (functionp '(1 2 3)))
  79. (true-or-false? ___ (functionp t)))
  80. (define-test test-there-are-some-other-type-predicates
  81. ; see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node73.html for more.
  82. (true-or-false? ___ (numberp 999))
  83. (true-or-false? ___ (listp '(9 9 9)))
  84. (true-or-false? ___ (integerp 999))
  85. (true-or-false? ___ (rationalp 9/99))
  86. (true-or-false? ___ (floatp 9.99))
  87. (true-or-false? ___ (stringp "nine nine nine"))
  88. (true-or-false? ___ (characterp #\9))
  89. (true-or-false? ___ (bit-vector-p #*01001)))
  90. (define-test test-guess-that-type!
  91. (let ((x ____))
  92. (assert-true (subtypep x '(SIMPLE-ARRAY T (* 3 *))))
  93. (assert-true (subtypep x '(SIMPLE-ARRAY T (5 * *))))
  94. (assert-true (subtypep x '(SIMPLE-ARRAY ARRAY *)))
  95. (assert-true (typep (make-array '(5 3 9) :element-type 'STRING ) x))
  96. (assert-true (typep (make-array '(5 3 33) :element-type 'VECTOR ) x))))