-
Notifications
You must be signed in to change notification settings - Fork 566
/
Copy pathvariables-parameters-constants.lisp
88 lines (73 loc) · 2.87 KB
/
variables-parameters-constants.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;; Copyright 2013 Google Inc.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(defun test-variable-assignment-with-setf ()
;; the let pattern allows us to create local variables with
;; lexical scope.
(let (var_name_1 (var_name_2 "Michael"))
;; variables may be defined with or without initial values.
(and
(equalp var_name_2 "Michael")
; new values may be assigned to variables with setf
(setf var_name_2 "Janet")
(equalp var_name_2 "Janet")
; setf may assign multiple variables in one form.
(setf var_name_1 "Tito"
var_name_2 "Jermaine")
(equalp var_name_1 "Tito")
(equalp var_name_2 "Jermaine"))))
(defun test-setf-for-lists ()
;; setf also works on list elements
(let (l)
(setf l '(1 2 3))
(equalp l '(1 2 3))
; First second and third are convenient accessor functions
; referring to the elements of a list
; For those interested, they are convenient to car, cadr, and caddr
(setf (first l) 10)
(setf (second l) 20)
(setf (third l) 30)
(equalp l '(10 20 30))))
(defparameter param_name_1 "Janet")
; defparameter requires an initial form. It is a compiler error to exclude it
;(defparameter param_no_init) ;; this will fail
(defconstant additive_identity 0)
; defconstant also requires an initial form
; (defconstant constant_no_init)
; reassigning parameters to new values is also ok, but parameters carry the
; connotation of immutability. If it's going to change frequently, it should
; be a var.
(setf param_name_1 "The other one")
; reassigning a constant is an error.
; this should result in a compile time error
; (setf additive_identity -1)
;; -------------------------------
;; below is necessary to run tests.
;; -------------------------------
(defvar failed-test-names nil)
(defun run-test (testfun)
(let ((fun-name (function-name testfun)))
(if (apply testfun '())
(format t ".")
(progn
(setf failed-test-names (cons fun-name failed-test-names))
(format t "F")))))
(defun function-name (function) (nth-value 2 (function-lambda-expression function)))
(run-test #'test-variable-assignment-with-setf)
(run-test #'test-setf-for-lists)
(format t "~%")
(defun report-failure (test-name)
(format t "~S failed.~%" test-name))
(if (endp failed-test-names) ; no failed tests
(format t "all tests pass.~%")
(mapcar #'report-failure failed-test-names))