-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap9.lisp
142 lines (121 loc) · 4.32 KB
/
chap9.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;; ex 9.2
(defun draw-line (size)
(cond ((zerop size) (format t "~%"))
(t (format t "*")
(draw-line (- size 1)))))
;; ex 9.3
(defun draw-box (width height)
"Draws a box with the specified dimensions."
(cond ((zerop height) nil)
(t (draw-line width)
(draw-box width (- height 1)))))
;; ex 9.4
(defun ninety-nine-bottles (n)
(cond ((zerop n) (format t "Aww, no more beer :<~%~%"))
(t (format t "~s bottles of beer on the wall,~%" n)
(format t "~s bottles of beer!~%" n)
(format t "Take one down,~%")
(format t "Pass it around,~%")
(ninety-nine-bottles (- n 1)))))
;; ex 9.5
(defun print-board (l)
"Prints a tic tac toe board filled with all elements of the given arg."
(print-board-rec 1 l))
(defun print-board-rec (n l)
(cond ((eql n 9) (format t " ~a ~%" (if (null (car l)) " " (car l))))
((zerop (mod n 3))
(format t " ~a ~%" (if (null (car l)) " " (car l)))
(format t "-----------~%")
(print-board-rec (+ n 1) (cdr l)))
(t (format t " ~a |" (if (null (car l)) " " (car l)))
(print-board-rec (+ n 1) (cdr l)))))
;; ex 9.6
(defun gross-pay ()
"Calculates a workers gross income given his hourly salary and his total work hours."
(format t "~&Hourly wage (in dollars) and number of hours worked: ")
(let ((wage (read))
(hours (read)))
(format t "~&Your gross income is: ~s~%" (* wage hours))))
;; ex 9.7
(defun cookie-monster ()
"Reads from stdin until it reads the symbol cookie."
(format t "~&Give me cookie!!!")
(format t "~&Cookie? ")
(let ((input (read)))
(cond ((eq 'cookie input)
(format t "Thank you!...Munch munch munch...BURP"))
(t (format t "No want ~a...~%~%" input)
(cookie-monster)))))
(defun my-first-file ()
(with-open-file (stream "/home/johny/Projects/road_to_cl/hello.txt"
:direction :output)
(format stream "I should be careful as to not overwrite any important files...~%")))
;; ex 9.8
;; 1. They are different data structures, symbolses names are actually strings
;; 2. Strings are case sensitive and evaluate to themselves, while symbols evaluate to their variables and are case insensitive
;; ex 9.9
;; 1. aB
;; 2. always
;; broke
;; 3. alphabet
;; ex 9.10
(defun space-over (n)
"Prints n spaces."
(cond ((< n 0) (format t "Error!~%"))
((zerop n) (format t ""))
(t (format t " ")
(space-over (- n 1)))))
(defun plot-one-point (plotting-string y-val)
"Writes plotting-string on the y-valth column."
(space-over y-val)
(format t "~a~%" plotting-string))
(defun plot-points (plotting-string columns)
"Writes plotting-string on each column."
(mapcar #'(lambda (n) (plot-one-point plotting-string n)) columns))
(defun generate (m n)
"Returns the interval of numbers from m to n."
(generate-aux m n '()))
(defun generate-aux (m n ans)
(cond ((eql m n) (cons m ans))
(t (generate-aux m (- n 1) (cons n ans)))))
(defun square (n)
"Computes the square of n."
(* n n))
(defun make-graph ()
"Reads func, start, end and plotting-string from stdin and prints a graph."
(format t "~&Func: ")
(let ((func (read)))
(format t "~&Start: ")
(let ((start (read)))
(format t "~&End: ")
(let ((end (read)))
(format t "~&Plotting-string: ")
(let ((plotting-string (read)))
(plot-points plotting-string (mapcar func
(generate start end))))))))
;; ex 9.11 (has errors!)
(defun dot-print (l)
"Prints the given list so stdout using dot notation."
(format t "(")
(cond ((null (cdr l)) (format t "~s . NIL" (car l)))
((not (atom (car l)))
(dot-print (car l))
(format t " . ")
(dot-print (cdr l)))
(t (format t "~s . " (car l))
(dot-print (cdr l))))
(format t ")"))
;; ex 9.13: (a . b)
;; ex 9.14
;; 1. (foo . (foo . (foo . ...
;; 2. ((((((((((((((((...
;; ex 9.15
(defun hybrid-print (l)
"Prints a given list, using dot notation only when necessary."
(format t "(")
(hybrid-print-aux l))
(defun hybrid-print-aux (l)
(cond ((null (cdr l)) (format t "~s)" (car l)))
((atom (cdr l)) (format t "~s . ~s)" (car l) (cdr l)))
(t (format t "~s " (car l))
(hybrid-print-aux (cdr l)))))