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

strings.lsp 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-double-quoted-strings-are-strings
  15. (let ((my-string "do or do not"))
  16. (true-or-false? ___ (typep my-string 'string))
  17. "strings are the same thing as vectors of characters"
  18. (true-or-false? ___ (typep my-string 'array))
  19. (assert-equal (aref "meat" 2) (aref "fiesta" 5))
  20. "strings are not integers :p"
  21. (true-or-false? ___ (typep my-string 'integer))))
  22. (define-test test-multi-line-strings-are-strings
  23. (let ((my-string "this is
  24. a multi
  25. line string"))
  26. (true-or-false? ___ (typep my-string 'string))))
  27. (define-test test-escape-quotes
  28. (let ((my-string "this string has one of these \" in it"))
  29. (true-or-false? ___ (typep my-string 'string))))
  30. ; This test from common lisp cookbook
  31. (define-test test-substrings
  32. "since strings are sequences, you may use subseq"
  33. (let ((my-string "Groucho Marx"))
  34. (assert-equal "Marx" (subseq my-string 8))
  35. (assert-equal (subseq my-string 0 7) ____)
  36. (assert-equal (subseq my-string 1 5) ____)))
  37. (define-test test-accessing-individual-characters
  38. "char literals look like this"
  39. (true-or-false? ___ (typep #\a 'character))
  40. (true-or-false? ___ (typep "A" 'character))
  41. (true-or-false? ___ (typep #\a 'string))
  42. "char is used to access individual characters"
  43. (let ((my-string "Cookie Monster"))
  44. (assert-equal (char my-string 0) #\C)
  45. (assert-equal (char my-string 3) #\k)
  46. (assert-equal (char my-string 7) ___)))
  47. (define-test test-concatenating-strings
  48. "concatenating strings in lisp is a little cumbersome"
  49. (let ((a "this")
  50. (b "is")
  51. (c "unwieldly"))
  52. (assert-equal ___ (concatenate 'string a " " b " " c))))
  53. (define-test test-searching-for-characters
  54. "you can use position to detect characters in strings
  55. (or elements of sequences)"
  56. (assert-equal ___ (position #\b "abc"))
  57. (assert-equal ___ (position #\c "abc"))
  58. (assert-equal ___ (find #\d "abc")))
  59. (define-test test-finding-substrings
  60. "search finds subsequences"
  61. (let ((title "A supposedly fun thing I'll never do again"))
  62. (assert-equal 2 (search "supposedly" title))
  63. (assert-equal 12 (search "CHANGETHISWORD" title))))