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

lists.lsp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ;; based on python koans 'about_lists.py'
  15. ;; based also on "Lisp 3rd addition" ch. 17. "List storage, surgery and reclamation"
  16. (define-test test-creating-lists
  17. "lists can be created using the quote form, or the 'list' function"
  18. (let ((fruits nil)
  19. (some-evens nil))
  20. (setf fruits '(orange pomello clementine))
  21. (setf some-evens (list (* 2 1) (* 2 2) (* 2 3)))
  22. (assert-equal fruits ___)
  23. (assert-equal ___ (length fruits))))
  24. (define-test test-list-cons
  25. "cons CONStructs new lists, by prefixing some list with
  26. a new element like (cons new-element some-list)"
  27. (let ((nums nil))
  28. (setf nums (cons :one nums))
  29. (assert-equal '(:one) nums)
  30. (setf nums (cons :two nums))
  31. (assert-equal ___ nums)
  32. "lists can contain anything, even mixtures of different things"
  33. (setf nums (cons 333 nums))
  34. (assert-equal ___ nums)
  35. "lists can of course contain lists"
  36. (setf nums (cons '("the" "rest") nums))
  37. (assert-equal ___ nums)))
  38. (define-test test-push-pop
  39. (let ((stack '(10 20 30 40))
  40. (firstval nil))
  41. "push adds an element to the beginning of a list referred to by some symbol"
  42. (push "last" stack)
  43. (assert-equal '("last" 10 20 30 40) stack)
  44. "pop is the opposite of push.
  45. It removes and returns the first element of a list"
  46. (setf firstval (pop stack))
  47. (assert-equal "last" firstval)
  48. (assert-equal '(10 20 30 40) stack)
  49. (setf firstval (pop stack))
  50. (assert-equal ___ firstval)
  51. (assert-equal ___ stack)))
  52. (define-test test-append
  53. "append attatches one list ot the end of another."
  54. (assert-equal '(:a :b :c) (append '(:a :b) '(:c)))
  55. (let ((abc '(:a :b :c))
  56. (xyz '(:x :y :z))
  57. (abcxyz nil))
  58. (setf abcxyz (append abc xyz))
  59. (assert-equal ___ abc)
  60. (assert-equal ___ xyz)
  61. (assert-equal ___ abcxyz)))
  62. (define-test test-accessing-list-elements
  63. (let ((noms '("peanut" "butter" "and" "jelly")))
  64. (assert-equal "peanut" (first noms))
  65. (assert-equal ___ (second noms))
  66. (assert-equal ___ (fourth noms))
  67. "last returns a singleton list of the final element"
  68. (assert-equal ___ (last noms))
  69. (assert-equal "butter" (nth 1 noms)) ; k 1
  70. (assert-equal ___ (nth 0 noms))
  71. (assert-equal ___ (nth 2 noms))
  72. "'elt' is similar to 'nth', with the arguments reversed"
  73. (assert-equal ___ (elt noms 2))))
  74. (define-test test-slicing-lists
  75. (let ((noms '("peanut" "butter" "and" "jelly")))
  76. (assert-equal ___ (subseq noms 0 1))
  77. (assert-equal ___ (subseq noms 0 2))
  78. (assert-equal ___ (subseq noms 2 2))
  79. (assert-equal ___ (subseq noms 2))))
  80. (define-test test-list-breakdown
  81. "car (aka. 'first') returns the first value in a list"
  82. (assert-equal ___ (car '(1 2 3)))
  83. (assert-equal ___ (car nil))
  84. "cdr (aka. 'rest') refers to the remainder of the list,
  85. after the first element"
  86. (assert-equal ___ (cdr '(1 2 3)))
  87. (assert-equal ___ (cdr nil)))