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

evaluation.lsp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 http://psg.com/~dlamkins/sl/chapter03-02.html
  15. (define-test test-function-name-is-first-argument
  16. "In most imperative languages, the syntax of a function call has
  17. the function name succeeded by a list of arguments. In lisp,
  18. the function name and arguments are all part of the same list,
  19. with the function name the first element of that list."
  20. "in these examples, the function names are +, -, and *"
  21. (assert-equal ___ (+ 2 3))
  22. (assert-equal ___ (- 1 3))
  23. (assert-equal ___ (* 7 4))
  24. "'>' and '=' are the boolean functions (predicates) 'greater-than' and
  25. 'equal to'"
  26. (assert-equal ___ (> 100 4))
  27. (assert-equal ___ (= 3 3))
  28. "'NUMBERP' is a predicate which returns true if the argument is a number"
  29. (assert-equal ___ (numberp 5))
  30. (assert-equal ___ (numberp "five")))
  31. (define-test test-evaluation-order
  32. "Arguments to functions are evaluated before the function"
  33. (assert-equal ___ (* (+ 1 2) (- 13 10))))
  34. (define-test test-quoting-behavior
  35. "Preceding a list with a quote (') will tell lisp not to evalute a list.
  36. The quote special form suppresses normal evaluation, an instead returns
  37. the literal list.
  38. Evaluating the form (+ 1 2) returns the number 3,
  39. but evaluating the form '(+ 1 2) returns the list (+ 1 2)"
  40. (assert-equal ____ (+ 1 2))
  41. (assert-equal ____ '(+ 1 2))
  42. "'LISTP' is a predicate which returns true if the argument is a list"
  43. " the '(CONTENTS) form defines a list literal containing CONTENTS"
  44. (assert-equal ___ (listp '(1 2 3)))
  45. (assert-equal ___ (listp 100))
  46. (assert-equal ___ (listp "Word to your moms I came to drop bombs"))
  47. (assert-equal ___ (listp nil))
  48. (assert-equal ___ (listp (+ 1 2)))
  49. (assert-equal ___ (listp '(+ 1 2)))
  50. "equalp is an equality predicate"
  51. (assert-equal ___ (equalp 3 (+ 1 2)))
  52. "the '(xyz ghi) syntax is syntactic sugar for the (QUOTE (xyz ghi)) function."
  53. (true-or-false? ___ (equalp '(/ 4 0) (quote (/ 4 0)))))