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

mapcar-and-reduce.lsp 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-mapcar-basics
  15. "We can apply a function to each member
  16. of a list using mapcar."
  17. (defun times-two (x) (* x 2))
  18. (assert-equal '(2 4 6) (mapcar #'times-two '(1 2 3)))
  19. (assert-equal '(3 "little" "pigs") (mapcar #'first '((3 2 1)
  20. ("little" "small" "tiny")
  21. ("pigs" "hogs" "swine")))))
  22. (define-test test-mapcar-multiple-lists
  23. "The mapcar function can be applied to
  24. more than one list. It applies a function
  25. to successive elements of the lists."
  26. (assert-equal '(4 10 18) (mapcar #'* '(1 2 3) '(4 5 6)))
  27. (assert-equal '(("lisp" "koans") ("are" "fun")) (mapcar #'list '("lisp" "are") '("koans" "fun"))))
  28. (define-test test-transpose-using-mapcar
  29. "Replace WRONG-FUNCTION with the correct function (don't forget
  30. the #') to take the 'transpose'."
  31. (defun WRONG-FUNCTION-1 (&rest rest) rest)
  32. (defun transpose (L) (apply #'mapcar (cons #'WRONG-FUNCTION-1 L)))
  33. (assert-equal '((1 4 7)
  34. (2 5 8)
  35. (3 6 9))
  36. (transpose '((1 2 3)
  37. (4 5 6)
  38. (7 8 9))))
  39. (assert-equal '(("these" "pretzels" "are")
  40. ("making" "me" "thirsty"))
  41. (transpose '(("these" "making")
  42. ("pretzels" "me")
  43. ("are" "thirsty")))))
  44. (define-test test-reduce-basics
  45. "The reduce function applies uses a supplied
  46. binary function to combine the elements of a
  47. list from left to right."
  48. (assert-equal 10 (reduce #'+ '(1 2 3 4)))
  49. (assert-equal 64 (reduce #'expt '(2 3 2))))
  50. (define-test test-reduce-right-to-left
  51. "The keyword :from-end allows us to apply
  52. reduce from right to left."
  53. (assert-equal 10 (reduce #'+ '(1 2 3 4) :from-end t))
  54. (assert-equal 512 (reduce #'expt '(2 3 2) :from-end t)))
  55. (define-test test-reduce-with-initial-value
  56. "We can supply an initial value to reduce."
  57. (assert-equal 1 (reduce #'expt '(10 21 34 43) :initial-value 1))
  58. (assert-equal 0 (reduce #'expt '(10 21 34 43) :initial-value 0)))
  59. (defun WRONG-FUNCTION-2 (a b) (a))
  60. (defun WRONG-FUNCTION-3 (a b) (a))
  61. (define-test test-mapcar-and-reduce
  62. "mapcar and reduce are a powerful combination.
  63. insert the correct function names, instead of WRONG-FUNCTION-X
  64. to define an inner product."
  65. (defun inner (x y)
  66. (reduce #'+ (mapcar #'* x y)))
  67. (assert-equal 32 (inner '(1 2 3) '(4 5 6)))
  68. (assert-equal 310 (inner '(10 20 30) '(4 3 7))))