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