-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_conditionals.lsp
80 lines (57 loc) · 1.92 KB
/
07_conditionals.lsp
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
(defparameter *age* 18) ; Create variable age (same as defvar)
;;; Relational Operators > < >= <= =
;;; Check if age is greater than or equal to 18
(if (= *age* 18)
(format t "You can vote~%")
(format t "You can't vote~%"))
;;; How to check for not equal
(if (not (= *age* 18))
(format t "You can vote~%")
(format t "You can't vote~%"))
;;; Logical Operators : and, or, not
(if (and (>= *age* 18) (<= *age* 67))
(format t "Time for work~%")
(format t "Work if you want~%"))
(if (or (<= *age* 14) (>= *age* 67) )
(format t "You shouldn't work~%")
(format t "You should work~%"))
(defparameter *num* 2)
(defparameter *num-2* 2)
(defparameter *num-3* 2)
;;; You can execute multiple statements in an if with progn
(if (= *num* 2)
(progn
(setf *num-2* (* *num-2* 2))
(setf *num-3* (* *num-3* 3)))
(format t "Not equal to 2~%"))
(format t "*num-2* = ~d ~%" *num-2*)
(format t "*num-3* = ~d ~%" *num-3*)
;;; Case performs certain actions depending on conditions
(defun get-school (age)
(case age
(5 (print "Kindergarten"))
(6 (print "First Grade"))
(otherwise (print "middle school"))))
(get-school 5)
(get-school 6)
(get-school 7)
(terpri) ; Newline
;;; when allows you to execute multiple statements by default
(when (= *age* 18)
(setf *num-3* 18)
(format t "Go to college you're ~d ~%" *num-3*))
;;; With unless code is executed if the expression is false
(unless (not (= *age* 18))
(setf *num-3* 20)
(format t "Something Random ~%")
)
;;; cond is like if else if else
(defvar *college-ready* nil)
(cond
((>= *age* 18) ; If T do this
(setf *college-ready* 'yes)
(format t "Ready for College ~%"))
((< *age* 18) ; Else If T do this
(setf *college-ready* 'no)
(format t "Not Ready for College ~%"))
(t (format t "Don't Know ~%"))) ; Else do this by default (t is for true)