-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
385 lines (366 loc) · 25.6 KB
/
index.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en_us">
<head>
<title>Lisp Practice</title>
<meta charset="utf-8" />
<meta name="generator" content="Pelican" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/static/css/main.css" />
<link rel="stylesheet" href="/static/css/theme.css" />
<link rel="icon" href="/images/yazani/yazani_1_extracted_bg_big_eyes_cropped.png" type="image/png" />
<link rel="apple-touch-icon" href="/images/yazani/yazani_1_extracted_bg_big_eyes_cropped.png" type="image/png" />
<script src="/static/misc.js"></script>
<script src="/blog/banner_image.js"></script>
<meta name="tags" content="programming, lisp" />
<meta property="og:site_name" content="dragoncoder047’s blog" />
<meta property="og:title" content="Lisp Practice" />
<meta property="og:description" content="Today I decided I would have a little fun with Lisp. I wanted to try and make a program that you can type in a function and get a graph of it. I started off with a function that takes an $x$-value and a list of coefficients, and evaluates …" />
<meta property="og:image" content="/images/yazani/yazani_1_extracted_bg.png" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://dragoncoder047.github.io/blog/2022/lisp-practice" />
<meta property="og:locale" content="['']" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="dragoncoder047’s blog - Lisp Practice" />
<meta name="twitter:description" content="Today I decided I would have a little fun with Lisp. I wanted to try and make a program that you can type in a function and get a graph of it. I started off with a function that takes an $x$-value and a list of coefficients, and evaluates …" />
<meta name="twitter:image" content="/images/yazani/yazani_1_extracted_bg.png" />
<!-- PrismJS -->
<script src="/static/prism.js" data-autoloader-path="https://cdn.jsdelivr.net/npm/prismjs@v1.x/components/"></script>
<script src="/static/prism-runbutton.js"></script>
<script src="/phoo/prism-phoo.js"></script> <!-- /PrismJS -->
<!-- Katex -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.css" type="text/css" />
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/mhchem.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.js"></script>
<link href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/copy-tex.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/copy-tex.js"></script>
<script>
window.addEventListener("DOMContentLoaded", () => {
renderMathInElement(document.body,
{
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
{ left: "\\begin{align}", right: "\\end{align}", display: true },
]
}
);
});
</script> <!-- /Katex -->
</head>
<body class="match-braces rainbow-braces">
<header>
<a href="https://dragoncoder047.github.io/blog" class="flex-row"><div class="flex-row"><img src="/images/yazani/yazani_1_extracted_bg.png" style="max-height:10em" id="banner-image" /><div id="sitename-text"><h1>dragoncoder047’s blog</h1><h2>random thoughts about nonrandom things</h2></div></div></a>
<nav>
<ul>
<li><a href="https://dragoncoder047.github.io/blog/">Home</a></li>
<li><a href="https://dragoncoder047.github.io/blog/archives">Archives</a>
</li>
<li><a href="https://dragoncoder047.github.io/blog/tags">By tag</a>
</li>
<li><a href="/">Site root</a>
</li>
<li><a href="#">Projects</a>
<ul>
<li><a href="https://dragoncoder047.github.io/thuepaste">Thuepaste</a>
</li>
<li><a href="https://dragoncoder047.github.io/armdroid">Armdroid</a>
</li>
<li><a href="https://dragoncoder047.github.io/langton-music">Langton's Ant Music</a>
</li>
<li><a href="https://dragoncoder047.github.io/schemascii">Schemascii</a>
</li>
<li><a href="https://dragoncoder047.github.io/parasite">Parasite</a>
</li>
</ul>
</li>
<li>
<form action="https://www.google.com/search" method="GET">
<input name="q" type="search" placeholder="Search"></input>
<input type="hidden" name="as_sitesearch" value="https://dragoncoder047.github.io/blog"></input>
<input type="submit" value="Search"></input>
</form>
</li>
</ul>
</nav>
</header>
<main>
<h1><a href="https://dragoncoder047.github.io/blog/2022/lisp-practice" rel="bookmark" title="Permalink to this page">Lisp Practice</a></h1>
<div class="flex-row">
<span style="flex: 1">← Previous:
<a href="https://dragoncoder047.github.io/blog/2022/ulisp-thoughts">
uLisp Thoughts
</a>
</span>
<span>Next:
<a href="https://dragoncoder047.github.io/blog/2022/partcl">
ParTcl
</a> →
</span>
</div>
<div class="post-info">
Posted <time class="published" datetime="2022-09-28T00:00:00-04:00">Wed 28 September 2022</time>
<address>By
<a href="https://dragoncoder047.github.io/blog/">dragoncoder047</a>
</address>
<div class="tags">
Tags:
<a href="https://dragoncoder047.github.io/blog/tag/lisp">lisp</a>
<a href="https://dragoncoder047.github.io/blog/tag/programming">programming</a>
</div>
</div>
<p>Today I decided I would have a little fun with Lisp. I wanted to try and make a program that you can type in a function and get a graph of it.</p>
<p>I started off with a function that takes an $x$-value and a list of coefficients, and evaluates the polynomial defined by those coefficients. Here’s the function:</p>
<pre class="highlight"><code class="language-lisp">(defun poly (x a)
(let ((result 0) (n (length a)))
(dotimes (k n)
(setq result (+ (* (nth k a) (expt x (- n k 1))) result)))
result))</code></pre>
<p>And here’s using it to evaluate the polynomial $2x^{3} - x^{2} + 5x - 10$ at $x = 3$:</p>
<pre class="highlight"><code class="language-lisp">(poly 3 '(2 -1 5 -10))</code></pre>
<p>Then I thought how to turn each of those points into an ASCII-art graph.</p>
<p>The polt function uses two nested <code class="language-lisp highlight">dolist</code> loops iterating over a list of x-values and y-values, and then at each point, it has to decide domehow whether to plot a point (by returning “#” insread of the background character). The simplest solution is to only plot it if $y = f(x)$ and don’t otherwise, but this only works when lines cross exactly over the gridline.</p>
<p>After considering a number of other methods, I decided on the method that <a href="https://www.youtube.com/c/Mattbatwings">mattbatwings</a> used in <a href="https://www.youtube.com/watch?v=EvvWOaLgKVU">his Minecraft graphing calculator</a>. This represents a function as 3D surface in $x$ and $y$, and then plotting all the points where the surface crosses the $x$-$y$ plane at $z=0$.</p>
<p>This function makes that decision. It takes the function $f$, the $x$ and $y$ coordinates, and $d$ which represents the distance between gird lines.</p>
<pre class="highlight"><code class="language-lisp">(defun zerocross (f x y d)
(or
(eq (< 0 (funcall f x y)) (> 0 (funcall f (- x d) y)))
(eq (< 0 (funcall f x (- y d))) (> 0 (funcall f x y)))
(eq (< 0 (funcall f x y)) (> 0 (funcall f (+ x d) y)))
(eq (< 0 (funcall f x (+ y d))) (> 0 (funcall f x y)))))</code></pre>
<p>If there is a sign change in the function close to the point, it returns <code class="language-lisp highlight">t</code>, otherwise it returns <code class="language-lisp highlight">nil</code>.</p>
<p>Now all that’s left to do is iterate over the provided ranges and call <code>zerocross</code> at each x- and y-value, and if there is no zero crossing, return the “background” characters.</p>
<p><code>axis</code> returns a line character if it is on the axis, and dots for a grid at increments of 2.</p>
<pre class="highlight"><code class="language-lisp">(defun axis (x y)
(if (and (= 0 x) (= 0 y)) "+"
(if (= 0 x) "|"
(if (= 0 y) "-"
(if (and (= 0 (mod x 2)) (= 0 (mod y 2))) "." " ")))))</code></pre>
<p><code>range</code> is an emulation fo the Python <code class="language-py3 highlight">range</code> function, which returns a list of numbers from <code>min</code> up to <code>max</code> in increments of <code>step</code>.</p>
<pre class="highlight"><code class="language-lisp">(defun range (min max step)
(let ((l nil) (c min))
(loop
(push c l)
(setq c (+ c step))
(when (>= c max) (return l)))))</code></pre>
<p><code>plot</code> then iterates over the entire x-y plane within the given range, and plots the function:</p>
<pre class="highlight"><code class="language-lisp">(defun plot (fx tx fy ty s fun)
(with-output-to-string (out)
(dolist (y (range fy ty s))
(dolist (x (range fx tx s))
(princ (if (zerocross fun x y s) "#" (axis x y)) out))
(terpri out))))</code></pre>
<p>If you’re interested in the whole code, here it is: <a download="plot.lisp" href="https://dragoncoder047.github.io/blog/2022/lisp-practice/plot.lisp">plot.lisp</a></p>
<p>Here’s using it to plot the function $y^{3} + x^{2} - 10x - 70 = 0$:</p>
<pre class="highlight"><code class="language-lisp">(princ
(plot
-20 20 ; x-values
-20 20 ; y-values
1 ; step in each direction
(lambda (x y) (+ (poly x '(1 -10 0)) (poly y '(1 0 0 -70))))))</code></pre>
<p>This produces this neat graph:</p>
<pre class="highlight"><code class="language-txt"> |
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
###########
. . .#################. . . . . . . . .
#### |####
. .## . . . . . . | . ##. . . . . . . .
## | ##
----##-------------+---##---------------
## | ##
. ##. . . . . . . | . .## . . . . . . .
## | ##
###. . . . . . . . | . . .###. . . . . .
## | #####
. . . . . . . . . | . . . . ######. . .
| #######
. . . . . . . . . | . . . . . . . #####
| #
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .
|
. . . . . . . . . | . . . . . . . . . .</code></pre>
<p>Pretty neat! An easy self-introduction to a programming language I though I’d never learn. Maybe I’ll take some time to learn more.</p>
<p>And here’s a bigger example: $x\sin\left(0.2\pi x\right)-y=0$:</p>
<pre class="highlight"><code class="language-lisp">(princ
(plot
-50 50 ; x-values
-50 50 ; y-values
1 ; step in each direction
(lambda (x y) (- (* x (sin (* x pi 0.2))) y))))</code></pre>
<pre class="highlight"><code class="language-txt"> |
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .
|
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .
|
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .
|
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .
# | #
. . ### . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . ### . . .
#### | ####
. . ####. . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . .#### . . .
#### | ####
. . ####. . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . .#### . . .
#### | ####
. . ####. . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . .#### . . .
#### | ####
. . ####. . . .#. . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . .#. . . .#### . . .
#### ### | ### ####
. . ####. . . ####. . . . . . . . . . . . . . . | . . . . . . . . . . . . . . .#### . . .#### . . .
#### #### | #### #### #
. . ####. . . ####. . . . . . . . . . . . . . . | . . . . . . . . . . . . . . .#### . . .#### . . #
#### #### | #### #### #
. . ####. . . ####. . . . . . . . . . . . . . . | . . . . . . . . . . . . . . .#### . . .#### . . #
## ## #### | #### ## ## #
. .## .## . . ####. . . . . . . . . . . . . . . | . . . . . . . . . . . . . . .#### . . ##. ##. . #
## ## #### | #### ## ## #
. .## .## . . ####. . . .#. . . . . . . . . . . | . . . . . . . . . . .#. . . .#### . . ##. ##. . #
## ## #### ### | ### #### ## ## #
. .## .## . . ####. . . ####. . . . . . . . . . | . . . . . . . . . .#### . . .#### . . ##. ##. . #
## ## ## ## #### | #### ## ## ## ## #
. .## .## . .## .## . . ####. . . . . . . . . . | . . . . . . . . . .#### . . ##. ##. . ##. ##. . #
## ## ## ## #### | #### ## ## ## ## #
. .## .## . .## .## . . ####. . . . . . . . . . | . . . . . . . . . .#### . . ##. ##. . ##. ##. . #
## ## ## ## #### | #### ## ## ## ## #
. .## .## . .## .## . .## ##. . . . . . . . . . | . . . . . . . . . .## ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## # | # ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . . ### . . . . . | . . . . . ### . . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## #### | #### ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . . ####. . . . . | . . . . .#### . . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## #### | #### ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . .## ##. . . . . | . . . . .## ##. . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . .## .## . . . . | . . . . ##. ##. . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . .## .## . . . . | . . . . ##. ##. . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ## ## #
. .## .## . .## .## . .## .## . .## .## . .#### | ####. . ##. ##. . ##. ##. . ##. ##. . ##. ##. . #
## ## ## ## ## ## ## ## ## ##|## ## ## ## ## ## ## ## ## ## #
#---##---##--##----##---##---##---##---##---##--###--##---##---##---##---##---##----##--##---##---##
# ## ## ## ## ## ## ## ## ## # ## ## ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . ##. ##. . ##. ##. . | . .## .## . .## .## . .## .## . .## .## . .## .##
# ## ## ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . ##. ##. . ##.## . . | . . ##.## . .## .## . .## .## . .## .## . .## .##
# ## ## ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . ##. ##. . .#### . . | . . ####. . .## .## . .## .## . .## .## . .## .##
# ## ## ## ## ## ## ## ### | ### ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . ##. ##. . . # . . . | . . . # . . .## .## . .## .## . .## .## . .## .##
# ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . ##.## . . . . . . . | . . . . . . . ##.## . .## .## . .## .## . .## .##
# ## ## ## ## ## ## ## | ## ## ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . .#### . . . . . . . | . . . . . . . ####. . .## .## . .## .## . .## .##
# ## ## ## ## ## #### | #### ## ## ## ## ## ##
#. ##. . ##. ##. . ##. ##. . .#### . . . . . . . | . . . . . . . ####. . .## .## . .## .## . .## .##
# ## ## ## ## ## #### | #### ## ## ## ## ## ##
#. ##. . ##. ##. . ##.## . . .#### . . . . . . . | . . . . . . . ####. . . ##.## . .## .## . .## .##
# ## ## ## ## ## ### | ### ## ## ## ## ## ##
#. ##. . ##. ##. . .#### . . . # . . . . . . . . | . . . . . . . . # . . . ####. . .## .## . .## .##
# ## ## ## #### | #### ## ## ## ##
#. ##. . ##. ##. . .#### . . . . . . . . . . . . | . . . . . . . . . . . . ####. . .## .## . .## .##
# ## ## ## #### | #### ## ## ## ##
#. ##. . ##.## . . .#### . . . . . . . . . . . . | . . . . . . . . . . . . ####. . . ##.## . .## .##
# ## #### #### | #### #### ## ##
#. ##. . .#### . . .#### . . . . . . . . . . . . | . . . . . . . . . . . . ####. . . ####. . .## .##
# ## #### #### | #### #### ## ##
#. ##. . .#### . . .###. . . . . . . . . . . . . | . . . . . . . . . . . . .###. . . ####. . .## .##
# ## #### # | # #### ## ##
#.## . . .#### . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . ####. . . ##.##
#### #### | #### ####
#### . . .#### . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . ####. . . ####.
#### #### | #### ####
#### . . .#### . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . ####. . . ####.
#### #### | #### ####
#### . . .#### . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . ####. . . ####.
#### #### | #### ####
#### . . .###. . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . .###. . . ####.
#### # | # ####
#### . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . ####.
#### | ####
#### . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . ####.
#### | ####
#### . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . ####.
#### | ####
#### . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . ####.
### | ###
# . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . # .
|
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .
|
. . . . . . . . . . . . . . . . . . . . . . . . | . . . . . . . . . . . . . . . . . . . . . . . . .</code></pre>
<hr />
<p><strong>Related Posts</strong></p>
<ul>
<li><a href="https://dragoncoder047.github.io/blog/2024/pointer-soup">Pointer Soup</a></li>
<li><a href="https://dragoncoder047.github.io/blog/2023/manual-memory-management-madness">Manual Memory Management Madness</a></li>
<li><a href="https://dragoncoder047.github.io/blog/2025/one-hell-of-a-physics-engine">One Hell of a Physics Engine</a></li>
<li><a href="https://dragoncoder047.github.io/blog/2025/so-far-ahead-yet-so-far-behind">So far ahead, yet so far behind</a></li>
<li><a href="https://dragoncoder047.github.io/blog/2024/boy-have-i-been">Boy, Have I Been...</a></li>
</ul>
<script src="https://giscus.app/client.js"
data-repo="dragoncoder047/blog"
data-repo-id="R_kgDOHCL60w"
data-category="Post Comments"
data-category-id="DIC_kwDOHCL6084CRxCW"
data-mapping="og:title"
data-reactions-enabled="1"
data-input-position="top"
data-theme="dark"
data-lang="en"
crossorigin="anonymous"
async
></script>
<section id="extras">
<div class="blogroll">
<ul>
<li><a href="https://www.conwaylife.com/">Conwaylife.com Forums</a></li>
<li><a href="https://www.python.org/">Python</a></li>
<li><a href="http://www.ulisp.com/">uLisp</a></li>
</ul>
</div>
<div class="social">
<ul>
<li><a href="https://github.com/dragoncoder047">dragoncoder047 on GitHub</a></li>
<li><a href="https://youtube.com/@dragoncoder047">dragoncoder047 on YouTube</a></li>
<li><a href="https://instagram.com/dragoncoder047/">dragoncoder047 on Instagram</a></li>
</ul>
</div>
</section>
</main>
<footer>
<address>
Site built by <a href="https://getpelican.com/">Pelican</a>
</address>
<a href="#" onclick="window.scrollTo({top: 0, left: 0});">Back to top</a>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XR0F89CCGK"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-XR0F89CCGK");
</script>
</footer>
</body>
</html>