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

iteration.lsp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ;; There are many options for iteration in lisp.
  15. ;; This set of koans will introduce a few of the most common ones
  16. ;; Dolist evaluates a form for every element om a list.
  17. (defvar some-primes '(10301 11311 19991 999565999))
  18. (define-test test-dolist
  19. "'dolist' iterates over values in a list, binding each value to a lexical
  20. variable in turn"
  21. (let ((how-many-in-list 0)
  22. (biggest-in-list (first some-primes)))
  23. "this dolist loops over the some-primes, defined above"
  24. (dolist (one-prime some-primes)
  25. (if (> one-prime biggest-in-list)
  26. (setf biggest-in-list one-prime))
  27. (incf how-many-in-list))
  28. (assert-equal ___ how-many-in-list)
  29. (assert-equal ___ biggest-in-list))
  30. (let ((sum 0))
  31. "write your own do-list here to calculate the sum of some-primes"
  32. "you may be interested in investigating the 'incf' function"
  33. ;(dolist ... )
  34. (assert-equal 999607602 sum)))
  35. (define-test test-dolist-with-return
  36. "Dolist can accept a return variable, which will be the return value
  37. upon completion of the iteration."
  38. (let ((my-list '(1 2 3 4))
  39. (my-return))
  40. (dolist (x my-list my-return)
  41. (push (* x x) my-return))
  42. (assert-equal ____ my-return)))
  43. (define-test test-dotimes
  44. "'dotimes' iterates over the integers from 0 to (limit - 1),
  45. binding them in order to your selected symbol."
  46. (let ((out-list nil))
  47. (dotimes (y 3) (push y out-list))
  48. (assert-equal out-list ___)))
  49. (defvar *x* "global")
  50. (define-test test-dotimes-binding
  51. "dotimes establishes a local lexical binding which may shadow
  52. a global value."
  53. (dotimes (*x* 4)
  54. (true-or-false? ___ (equal "global" *x*)))
  55. (true-or-false? ___ (equal "global" *x*)))
  56. (define-test test-loop-until-return
  57. "Loop loops forever, unless some return condition is executed.
  58. Note that the loop macro includes many additional options,
  59. which will be covered in a future koan."
  60. (let ((loop-counter 0))
  61. (loop
  62. (incf loop-counter)
  63. (if (>= loop-counter 100) (return loop-counter)))
  64. (assert-equal ___ loop-counter)))
  65. (define-test test-mapcar
  66. "mapcar takes a list an a function. It returns a new list
  67. with the function applied to each element of the input"
  68. (let ((mc-result (mapcar #'evenp '(1 2 3 4 5))))
  69. (assert-equal mc-result ____)))
  70. ;; ----
  71. (defun vowelp (c)
  72. "returns true iff c is a vowel"
  73. (find c "AEIOUaeiou"))
  74. (defun vowels-to-xs (my-string)
  75. "converts all vowels in a string to the character 'x'"
  76. (coerce
  77. (loop for c across my-string
  78. with new-c
  79. do (setf new-c (if (vowelp c) #\x c))
  80. collect new-c)
  81. 'string))
  82. (define-test test-mapcar-with-defun
  83. "mapcar is a convenient way to apply a function to a collection"
  84. (assert-equal (vowels-to-xs "Astronomy") "xstrxnxmy")
  85. (let* ((subjects '("Astronomy" "Biology" "Chemistry" "Linguistics"))
  86. (mc-result (mapcar #'vowels-to-xs subjects)))
  87. (assert-equal mc-result ____)))
  88. ;; ----
  89. (define-test test-mapcar-with-lambda
  90. (let ((mc-result (mapcar #'(lambda (x) (mod x 10)) '(21 152 403 14))))
  91. (assert-equal mc-result ____)))