-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial2.html
More file actions
766 lines (662 loc) · 39.6 KB
/
tutorial2.html
File metadata and controls
766 lines (662 loc) · 39.6 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
<title>Slimv Tutorial - Part Two</title>
</head>
<body>
<style id='page-skin-1' type='text/css'>
<!--
body {
width: 800px;
text-align:justify;
font:normal normal 100% Georgia,Serif;
background:#ffffff;
color:#000000;
}
a:link {
color:#5588aa;
text-decoration:none;
}
a:visited {
color:#999999;
text-decoration:none;
}
a img {
border-width:0;
}
#content {
position:absolute;
top:0;
left:20px;
width: 700px;
padding-right: 20px;
border-right: 1px solid gray;
}
.nav-prev {
float: left;
text-align: left;
}
.nav-next {
float: right;
text-align: right;
}
h1 {
border-bottom:1px;
margin-bottom:0px;
color: #000000;
font: normal bold 200% Georgia,Serif;
}
h1 a, h1 a:link, h1 a:visited {
color: #000000;
}
#main .Header {
border-bottom-width: 0px;
}
h3.post-title {
font-size: 140%;
color: #000000;
}
.post {
padding-left:0%;
padding-right:0%;
}
.clear {
clear: both;
}
-->
</style>
<style type="text/css">
<!--
.fixed {
font:normal bold 100% monospace;
}
-->
</style>
<style type="text/css">
<!--
.lisp-body {
color: #c0c0c0;
background-color: #400040;
padding: 10px;
font-size: 9pt;
}
.inverse {
color: #000000;
background-color: #ffffff;
}
.builtin {
/* font-lock-builtin-face */
color: #b0c4de;
}
.keyword {
/* font-lock-keyword-face */
color: #ffffff;
}
.string {
/* font-lock-string-face */
color: #00ffff;
}
.number {
/* font-lock-number-face */
color: #00ffff;
}
.type {
/* font-lock-type-face */
color: #ffff00;
}
.paren1 {
/* font-lock-paren1-face */
color: #ff0000;
}
.paren2 {
/* font-lock-paren2-face */
color: #ff8000;
}
.paren3 {
/* font-lock-paren3-face */
color: #ffff00;
}
.paren4 {
/* font-lock-paren4-face */
color: #80ff00;
}
.paren5 {
/* font-lock-paren5-face */
color: #00ff00;
}
.paren6 {
/* font-lock-paren6-face */
color: #00ffff;
}
a {
color: inherit;
background-color: inherit;
font: inherit;
text-decoration: inherit;
}
-->
</style>
<style type="text/css">
<!--
.repl-body {
color: #c0c0c0;
background-color: #000000;
padding: 10px;
font-size: 9pt;
}
.inverse {
color: #000000;
background-color: #ffffff;
}
.source {
color: #c0c0c0;
background-color: #400040;
}
.highlight {
color: #fff8dc;
background-color: #808080;
}
.prompt {
/* font-lock-prompt-face */
color: #ffff00;
}
.manual {
/* font-lock-manual-face */
color: #ffffff;
}
.comment {
/* font-lock-comment-face */
color: #00ff00;
}
.explain {
/* font-lock-explain-face */
color: #ff0000;
}
a {
color: inherit;
background-color: inherit;
font: inherit;
text-decoration: inherit;
}
-->
</style>
<style type="text/css">
body{
margin:0;
padding:0 0 0 150px;
}
div#sidebar{
position:absolute;
top:0;
left:760px;
width:180px;
height:100%;
font:normal normal 75% Georgia,Serif;
text-align:left;
}
* html body{
overflow:hidden;
}
</style>
<div id='content'>
<h1 class='title'>
<a href='https://github.com/kovisoft/slimv'>Slimv Tutorial</a> - Part Two
</h1>
<hr />
<h3 class='post-title entry-title'>
</h3>
<p>This is the second part of the <a href='tutorial.html'>Slimv Tutorial series</a>. We keep on performing Marco Barringer's actions from his <a href='http://common-lisp.net/project/movies/movies/slime.mov'>SLIME tutorial movie</a> using Vim and the <a href='http://www.vim.org/scripts/script.php?script_id=2531'>Slimv plugin</a>.</p>
<p>This part of the tutorial assumes that you have followed <a href='tutorial.html'>Part One</a> and your REPL is loaded with all the definitions added there. So if you haven't yet visited <a href='tutorial.html'>Part One</a>, please do so before continuing with this page.</p>
<h3 class='post-title entry-title'>
<a NAME="sldb">Using the SLIME debugger
</h3>
<p>Having written the morse code lookup function in <a href='tutorial.html'>Part One</a>, now let's write the reverse function, then evaluate it with <span class="fixed">,d</span> (or alternatively save and compile the file morse.lisp we are working on).</p>
<pre class="lisp-body">
<span class="paren1">(</span><span class="keyword">defun</span> morse-to-character <span class="paren2">(</span>morse-string<span class="paren2">)</span>
<span class="paren2">(</span><span class="keyword">first</span> <span class="paren3">(</span><span class="keyword">find</span> morse-string <span class="keyword">*morse-mapping* :test #'string= :key #'cdr</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<p>Switch to the REPL buffer (<span class="fixed"><Ctrl-w>w</span>) and test the new function by typing the yellow text shown below. Remember that for <span class="fixed">morse-to-character</span> it is enough to type <span class="fixed">mtc<Tab></span> and fuzzy completion completes the symbol name.<br>
Note that not only the parens but also the double quotes are handled in a balanced way by the Paredit mode, so you don't need to type the closing <span class="fixed">"</span> and <span class="fixed">)</span> characters, they are inserted automatically. You can press <span class="fixed">Enter</span> (in Insert mode) anywhere on the line to evaluate it:</p>
<pre class="repl-body">
<span class="prompt">MORSE></span> <span class="paren1">(</span>character-to-morse <span class="keyword">#\m</span><span class="paren1">)</span>
<span class="string">"--"</span>
<span class="prompt">MORSE></span> <span class="paren1">(</span>morse-to-character <span class="string">"--"</span><span class="paren1">)</span>
<span class="inverse">Slimv.REPL.lisp 12,0-1 All</span>
The value (".-")
is not of type
(OR (VECTOR CHARACTER) (VECTOR NIL) BASE-STRING SYMBOL
CHARACTER).
[Condition of type TYPE-ERROR]
<span class="inverse">R</span>estarts:
<span class="keyword">0:</span> [RETRY] Retry SLIME REPL evaluation request.
<span class="keyword">1:</span> [*ABORT] Return to SLIME's top level.
<span class="keyword">2:</span> [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" RUNNING ...
Backtrace:
<span class="keyword">0:</span> (STRING= "--" (".-"))[:EXTERNAL]
<span class="keyword">1:</span> (FIND "--" ((#\\A ".-") (#\\B "-...") (#\\C "-.-.") (#\\D "-..") (#\\E ".")...
<span class="keyword">2:</span> (MORSE-TO-CHARACTER "--")
<span class="keyword">3:</span> (SB-INT:SIMPLE-EVAL-IN-LEXENV (MORSE-TO-CHARACTER "--") #<NULL-LEXENV>)
<span class="keyword">4:</span> (SWANK::EVAL-REGION "(morse-to-character \\"--\\")")
<span class="keyword">5:</span> ((LAMBDA ()))
<span class="keyword">6:</span> (SWANK::TRACK-PACKAGE #<CLOSURE (LAMBDA #) {B1B7815}>)
<span class="keyword">7:</span> (SWANK::CALL-WITH-RETRY-RESTART "Retry SLIME REPL evaluation request."...
<span class="keyword">8:</span> (SWANK::CALL-WITH-BUFFER-SYNTAX NIL #<CLOSURE (LAMBDA #) {B1B778D}>)
<span class="keyword">9:</span> (SWANK::REPL-EVAL "(morse-to-character \\"--\\")")
<span class="keyword">10:</span> (SB-INT:SIMPLE-EVAL-IN-LEXENV (SWANK:LISTENER-EVAL "(morse-to-character ...
<span class="inverse">Slimv.SLDB [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^7,1^^^^^^^^^^^^Top</span>
</pre>
<p>Oops, something went wrong. Welcome to the SLIME debugger (SLDB in short). In Marco's words: <i>"this is your best friend, your worst enemy, and everything in between"</i>.</p>
<p>The <span class="fixed">Restarts:</span> section displays the possible restarts, the <span class="fixed">Backtrace:</span> section displays the actual call stack. If you place the cursor on a numbered line in the <span class="fixed">Backtrace:</span> section and press <span class="fixed">Enter</span> (in Normal mode) then the frame locals are displayed:</p>
<pre class="repl-body">
The value (".-")
is not of type
(OR (VECTOR CHARACTER) (VECTOR NIL) BASE-STRING SYMBOL
CHARACTER).
[Condition of type TYPE-ERROR]
Restarts:
<span class="keyword">0:</span> [RETRY] Retry SLIME REPL evaluation request.
<span class="keyword">1:</span> [*ABORT] Return to SLIME's top level.
<span class="keyword">2:</span> [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" RUNNING ...
Backtrace:
<span class="inverse"> </span> <span class="keyword">0:</span> (STRING= "--" (".-"))[:EXTERNAL]
No source line information
Locals:
SB-DEBUG::ARG-0 = 2
SB-DEBUG::ARG-1 = "--"
SB-DEBUG::ARG-2 = (".-")
<span class="keyword">1:</span> (FIND "--" ((#\\A ".-") (#\\B "-...") (#\\C "-.-.") (#\\D "-..") (#\\E ".")...
No source line information
Locals:
SB-DEBUG::ARG-0 = 6
SB-DEBUG::ARG-1 = "--"
SB-DEBUG::ARG-2 = ((#\\A ".-") (#\\B "-...") (#\\C "-.-.") (#\\D "-..") ....
<span class="keyword">2:</span> (MORSE-TO-CHARACTER "--")
No source line information
Locals:
SB-DEBUG::ARG-0 = "--"
<span class="keyword">3:</span> (SB-INT:SIMPLE-EVAL-IN-LEXENV (MORSE-TO-CHARACTER "--") #<NULL-LEXENV>)
<span class="keyword">4:</span> (SWANK::EVAL-REGION "(morse-to-character \\"--\\")")
<span class="inverse">Slimv.SLDB [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^13,1^^^^^^^^^^^^Top</span>
</pre>
<p>From the call stack we can see that a list containing a string was passed to <span class="fixed">string=</span> as a second argument. The <span class="fixed">:key</span> must be <span class="fixed">#'second</span> instead of <span class="fixed">#'cdr</span> in our <span class="fixed">morse-to-character</span> function.</p>
<p>If you press <span class="fixed">Enter</span> on a numbered line in the <span class="fixed">Restarts:</span> section then the corresponding restart is executed. The most frequently used restarts have their own keyboard shortcuts: <span class="fixed">,a</span> selects the Abort, <span class="fixed">,q</span> selects the Quit and <span class="fixed">,n</span> selects the contiNue restart. Let's choose restart <span class="fixed">1: [*ABORT] Return to SLIME's top level</span> by pressing <span class="fixed">,a</span>:</p>
<pre class="repl-body">
<span class="prompt">MORSE></span>
<span class="explain">;; Pressing ,a for selecting the Abort restart.</span>
<span class="comment">; Evaluation aborted on NIL</span>
<span class="prompt">MORSE></span> <span class="comment">; Quit to level 1</span>
<span class="prompt">MORSE></span> <span class="comment">; Evaluation aborted on #<TYPE-ERROR {BBFD8F1}></span>
</pre>
<p>Switch to the source code and fix the function, then re-evaluate and re-test it:</p>
<pre class="lisp-body">
<span class="paren1">(</span><span class="keyword">defun</span> morse-to-character <span class="paren2">(</span>morse-string<span class="paren2">)</span>
<span class="paren2">(</span><span class="keyword">first</span> <span class="paren3">(</span><span class="keyword">find</span> morse-string <span class="keyword">*morse-mapping* :test #'string= :key #'second</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<pre class="repl-body">
<span class="prompt">MORSE></span>
<span class="paren1">(</span><span class="keyword">defun</span> morse-to-character <span class="paren2">(</span>morse-string<span class="paren2">)</span>
<span class="paren2">(</span><span class="keyword">first</span> <span class="paren3">(</span><span class="keyword">find</span> morse-string <span class="keyword">*morse-mapping* :test #'string= :key #'second</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
STYLE-WARNING: redefining MORSE-TO-CHARACTER in DEFUN
MORSE-TO-CHARACTER
<span class="prompt">MORSE></span> <span class="paren1">(</span>morse-to-character <span class="string">"--"</span><span class="paren1">)</span>
<span class="keyword">#\M</span>
<span class="prompt">MORSE></span> <span class="paren1">(</span>morse-to-character <span class="string">".-"</span><span class="paren1">)</span>
<span class="keyword">#\A</span>
<span class="prompt">MORSE></span> <span class="paren1">(</span>morse-to-character <span class="string">"..--.."</span><span class="paren1">)</span>
<span class="keyword">#\?</span>
</pre>
<h3 class='post-title entry-title'>
<a NAME="debug">More debugging methods
</h3>
<p>Sometimes variables or function calls may be optimized away, so it's rather complicated to examine the backtrace. In this case we may add the following <span class="fixed">declaim</span> form at the top of the source file, this instructs lisp to produce the safest code and show all debug information:</p>
<pre class="lisp-body">
<span class="paren1">(</span><span class="keyword">declaim</span> <span class="paren2">(</span><span class="keyword">optimize</span> <span class="paren3">(</span><span class="keyword">speed</span> <span class="number">0</span><span class="paren3">)</span> <span class="paren3">(</span><span class="keyword">safety</span> <span class="number">3</span><span class="paren3">)</span> <span class="paren3">(</span><span class="keyword">debug</span> <span class="number">3</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<p>At this point in his movie Marco presents some Paredit trickery. We skip this topic for now, it will be covered in another part of the Slimv tutorial. Instead we go on adding some new functions, that are going to transform strings into morse sequences and back.</p>
<p>The first one is <span class="fixed">string-to-morse</span>. Here Marco demonstrates Slime's hierarchical compiler notes buffer by compiling a buggy version of the function. Unfortunately there is no compiler notes buffer in Slimv at the moment, rather all errors and warning messages are printed in the REPL buffer together with all compilation information supplied by the Swank server. Let's see an example of a problematic loop form:</p>
<pre class="lisp-body">
<span class="paren1">(</span><span class="keyword">defun</span> string-to-morse <span class="paren2">(</span><span class="keyword">string</span><span class="paren2">)</span>
<span class="paren2">(</span><span class="keyword">with-output-to-string</span> <span class="paren3">(</span>morse<span class="paren3">)</span>
<span class="paren3">(</span><span class="keyword">write-string</span> <span class="paren4">(</span>character-to-morse <span class="paren5">(</span><span class="keyword">aref string</span> <span class="number">0</span><span class="paren5">)</span><span class="paren4">)</span> morse<span class="paren3">)</span>
<span class="paren3">(</span><span class="keyword">loop
do</span>
for <span class="keyword">char</span> across <span class="paren4">(</span><span class="keyword">subseq string</span> <span class="number">1</span><span class="paren4">)</span>
<span class="keyword">do</span> <span class="paren4">(</span><span class="keyword">write-char #\Space morse</span><span class="paren4">)</span>
<span class="keyword">do</span> <span class="paren4">(</span><span class="keyword">write-string</span> <span class="paren5">(</span>character-to-morse <span class="keyword">char</span><span class="paren5">)</span> morse<span class="paren4">)</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<p>And this is the compilation result of <span class="fixed">string-to-morse</span>:</p>
<pre class="repl-body">
<span class="prompt">MORSE></span>
<span class="comment">; compiling file "/home/kovisoft/morse.lisp" (written 30 APR 2011 05:00:32 PM):
; file: /home/kovisoft/morse.lisp
; in: DEFUN STRING-TO-MORSE
; (LOOP DO
; MORSE::FOR CHAR MORSE::ACROSS (SUBSEQ STRING 1)
; DO (WRITE-CHAR #\ MORSE::MORSE)
; DO (WRITE-STRING (MORSE::CHARACTER-TO-MORSE CHAR) MORSE::MORSE))
;
; caught ERROR:
; (in macroexpansion of (LOOP DO FOR ...))
; (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
; A compound form was expected, but FOR found.
; current LOOP context: DO FOR CHAR.
; (WITH-OUTPUT-TO-STRING (MORSE::MORSE)
; (WRITE-STRING (MORSE::CHARACTER-TO-MORSE (AREF STRING 0)) MORSE::MORSE)
; (LOOP DO
; MORSE::FOR CHAR MORSE::ACROSS (SUBSEQ STRING 1)
; DO (WRITE-CHAR #\ MORSE::MORSE)
; DO (WRITE-STRING (MORSE::CHARACTER-TO-MORSE CHAR) MORSE::MORSE)))
; --> LET GET-OUTPUT-STREAM-STRING
; ==>
; MORSE::MORSE
;
; note: deleting unreachable code
;
; compilation unit finished
; caught 1 ERROR condition
; printed 1 note
; /home/kovisoft/morse.fasl written
; compilation finished in 0:00:00.090</span>
2 compiler notes:
/home/kovisoft/morse.lisp:1219
error: (in macroexpansion of (LOOP DO FOR ...))
(hint: For more precise location, try *BREAK-ON-SIGNALS*.)
A compound form was expected, but FOR found.
current LOOP context: DO FOR CHAR.
/home/kovisoft/morse.lisp:1120
note: deleting unreachable code
</pre>
<p>This long list informs us that there is a problem in the macroexpansion of <span class="fixed">(LOOP DO FOR ...)</span>. We can collect information about the proper usage of the <span class="fixed">loop</span> form in various ways.</p>
<p>One way is to hover the mouse over the <span class="fixed">loop</span> symbol: this will display a tooltip with the symbol description. Another way to describe a symbol is to place the cursor on the symbol name and press <span class="fixed">,s</span> (or select Documentation/Describe-Symbol from the Slimv menu):</p>
<pre class="repl-body">
COMMON-LISP:LOOP
[symbol]
LOOP names a macro:
Lambda-list: (&ENVIRONMENT ENV &REST KEYWORDS-AND-FORMS)
Source file: SYS:SRC;CODE;LOOP.LISP
<span class="comment">Press ENTER or type command to continue</span><span class="inverse"> </span>
</pre>
<p>We can also look up the symbol in the Common Lisp Hyperspec by pressing <span class="fixed">,h</span> (or selecting Documentation/Hyperspec from the Slimv menu). This will open the related Hyperspec page in the default browser.</p>
<p>But as we know that <span class="fixed">loop</span> is a macro, therefore we can also examine its macroexpansion by pressing <span class="fixed">,1</span> on the form (or selecting Debugging/Macroexpand-1 from the Slimv menu):</p>
<pre class="repl-body">
A compound form was expected, but FOR found.
current LOOP context: DO FOR CHAR.
[Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]
<span class="inverse">R</span>estarts:
<span class="keyword">0:</span> [*ABORT] Return to SLIME's top level.
<span class="keyword">1:</span> [TERMINATE-THREAD] Terminate this thread (#<THREAD "worker" RUNNING {B2C63D1}>)
Backtrace:
<span class="keyword">0:</span> (SB-LOOP::LOOP-ERROR "A compound form was expected, but ~S found.")[:EXTERNAL]
<span class="keyword">1:</span> (SB-LOOP::LOOP-GET-COMPOUND-FORM)
<span class="keyword">2:</span> (SB-LOOP::LOOP-GET-PROGN)
<span class="keyword">3:</span> (SB-LOOP::LOOP-DO-DO)
<span class="keyword">4:</span> (SB-LOOP::LOOP-ITERATION-DRIVER)
<span class="keyword">5:</span> (SB-LOOP::LOOP-TRANSLATE (DO FOR CHAR ACROSS (SUBSEQ STRING 1) ...
<span class="keyword">6:</span> (MACROEXPAND-1 ..)
<span class="inverse">Slimv.SLDB [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^5,1^^^^^^^^^^^^Top</span>
<span class="source"> <span class="inverse">(</span><span class="keyword">loop</span> </span>
<span class="source"> <span class="keyword">do</span> </span>
<span class="source"> for <span class="keyword">char</span> across <span class="paren4">(</span><span class="keyword">subseq string</span> <span class="number">1</span><span class="paren4">)</span> </span>
<span class="source"> </span>
<span class="inverse">morse.lisp 58,6 Bot</span>
</pre>
<p>Now we see that the problem is the extra <span class="fixed">do</span> before <span class="fixed">for</span>. Select the <span class="fixed">0: [*ABORT]</span> restart and eliminate the extra line:</p>
<pre class="lisp-body">
<span class="paren1">(</span><span class="keyword">defun</span> string-to-morse <span class="paren2">(</span><span class="keyword">string</span><span class="paren2">)</span>
<span class="paren2">(</span><span class="keyword">with-output-to-string</span> <span class="paren3">(</span>morse<span class="paren3">)</span>
<span class="paren3">(</span><span class="keyword">write-string</span> <span class="paren4">(</span>character-to-morse <span class="paren5">(</span><span class="keyword">aref string</span> <span class="number">0</span><span class="paren5">)</span><span class="paren4">)</span> morse<span class="paren3">)</span>
<span class="paren3">(</span><span class="keyword">loop</span>
for <span class="keyword">char</span> across <span class="paren4">(</span><span class="keyword">subseq string</span> <span class="number">1</span><span class="paren4">)</span>
<span class="keyword">do</span> <span class="paren4">(</span><span class="keyword">write-char #\Space morse</span><span class="paren4">)</span>
<span class="keyword">do</span> <span class="paren4">(</span><span class="keyword">write-string</span> <span class="paren5">(</span>character-to-morse <span class="keyword">char</span><span class="paren5">)</span> morse<span class="paren4">)</span><span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<p>After macroexpanding the loop form again (make sure that the cursor stays inside the the loop form, but not inside an inner sub-form) we get something like that:</p>
<pre class="repl-body">
<span class="prompt">MORSE></span>
<span class="explain">;; Macroexpand-1 was selected here on (loop ...)</span>
<span class="paren1">(</span>BLOCK NIL
<span class="paren2">(</span>LET <span class="paren3">(</span><span class="paren4">(</span>CHAR NIL<span class="paren4">)</span>
<span class="paren4">(</span>#:LOOP-ACROSS-VECTOR-862 <span class="paren5">(</span>SUBSEQ STRING 1<span class="paren5">)</span><span class="paren4">)</span>
<span class="paren4">(</span>#:LOOP-ACROSS-INDEX-863 0<span class="paren4">)</span>
<span class="paren4">(</span>#:LOOP-ACROSS-LIMIT-864 0<span class="paren4">)</span><span class="paren3">)</span>
<span class="paren3">(</span>DECLARE <span class="paren4">(</span>TYPE FIXNUM #:LOOP-ACROSS-LIMIT-864<span class="paren4">)</span>
<span class="paren4">(</span>TYPE FIXNUM #:LOOP-ACROSS-INDEX-863<span class="paren4">)</span>
<span class="paren4">(</span>TYPE VECTOR #:LOOP-ACROSS-VECTOR-862<span class="paren4">)</span><span class="paren3">)</span>
<span class="paren3">(</span>SB-LOOP::LOOP-BODY
<span class="paren4">(</span><span class="paren5">(</span>SETQ #:LOOP-ACROSS-LIMIT-864 <span class="paren6">(</span>LENGTH #:LOOP-ACROSS-VECTOR-862<span class="paren6">)</span><span class="paren5">)</span><span class="paren4">)</span>
<span class="paren4">(</span><span class="paren5">(</span>WHEN <span class="paren6">(</span>>= #:LOOP-ACROSS-INDEX-863 #:LOOP-ACROSS-LIMIT-864<span class="paren6">)</span>
<span class="paren6">(</span>GO SB-LOOP::END-LOOP<span class="paren6">)</span><span class="paren5">)</span>
<span class="paren5">(</span>SB-LOOP::LOOP-REALLY-DESETQ CHAR
<span class="paren6">(</span>AREF #:LOOP-ACROSS-VECTOR-862
#:LOOP-ACROSS-INDEX-863<span class="paren6">)</span><span class="paren5">)</span>
NIL
<span class="paren5">(</span>SB-LOOP::LOOP-REALLY-DESETQ #:LOOP-ACROSS-INDEX-863
<span class="paren6">(</span>1+ #:LOOP-ACROSS-INDEX-863<span class="paren6">)</span><span class="paren5">)</span><span class="paren4">)</span>
<span class="paren4">(</span><span class="paren5">(</span>WRITE-CHAR #\ MORSE<span class="paren5">) (</span>WRITE-STRING <span class="paren6">(</span>CHARACTER-TO-MORSE CHAR<span class="paren6">)</span> MORSE<span class="paren5">)</span><span class="paren4">)</span>
<span class="paren4">(</span><span class="paren5">(</span>WHEN <span class="paren6">(</span>>= #:LOOP-ACROSS-INDEX-863 #:LOOP-ACROSS-LIMIT-864<span class="paren6">)</span>
<span class="paren6">(</span>GO SB-LOOP::END-LOOP<span class="paren6">)</span><span class="paren4">)</span>
<span class="paren5">(</span>SB-LOOP::LOOP-REALLY-DESETQ CHAR
<span class="paren6">(</span>AREF #:LOOP-ACROSS-VECTOR-862
#:LOOP-ACROSS-INDEX-863<span class="paren6">)</span><span class="paren5">)</span>
NIL
<span class="paren5">(</span>SB-LOOP::LOOP-REALLY-DESETQ #:LOOP-ACROSS-INDEX-863
<span class="paren6">(</span>1+ #:LOOP-ACROSS-INDEX-863<span class="paren6">)</span><span class="paren5">)</span><span class="paren4">)</span>
NIL<span class="paren3">)</span><span class="paren2">)</span><span class="paren1">)</span>
</pre>
<p>Right now we don't decrypt the output, but at least we can confirm the macroexpansion does not give any errors. Let's compile the corrected function and test it in the REPL buffer:</p>
<pre class="repl-body">
<span class="prompt">MORSE></span> <span class="paren1">(</span>string-to-morse <span class="string">"marco"</span><span class="paren1">)</span>
<span class="string">"-- .- .-. -.-. ---"</span>
<span class="prompt">MORSE></span> <span class="paren1">(</span>string-to-morse <span class="string">"sos"</span><span class="paren1">)</span>
<span class="string">"... --- ..."</span>
</pre>
<h3 class='post-title entry-title'>
<a NAME="asdf">Installing a package
</h3>
<p>The next function to implement is <span class="fixed">morse-to-string</span>, for this we'll need a function that splits a delimited string: <span class="fixed">split-sequence:split-sequence</span>. Package <span class="fixed">split-sequence</span> is not part of our lisp by default, so we need to install it first, e.g. via <a href='http://common-lisp.net/project/asdf/'>ASDF</a>. Please note that the availability and usage of ASDF depends on the Common Lisp implementation.</p>
<pre class="repl-body">
<span class="prompt">CL-USER></span> <span class="paren1">(</span><span class="keyword">require</span> :asdf-install<span class="paren1">)</span>
<span class="paren1">(<span class="string">"ASDF-INSTALL"</span>)</span>
<span class="prompt">CL-USER></span> <span class="paren1">(</span>asdf-install:install :split-sequence<span class="paren1">)</span>
Install where?
1) System-wide install:
System in /usr/lib/sbcl/site-systems/
Files in /usr/lib/sbcl/site/
2) Personal installation:
System in /home/kovisoft/.sbcl/systems/
Files in /home/kovisoft/.sbcl/site/
--> <span class="manual">2</span>
Downloading 2601 bytes from http://ftp.linux.org.uk/pub/lisp/experimental/cclan/split...
<span class="inverse">Slimv.REPL.lisp 112,0-1 All</span>
The function ASDF::SPLIT is undefined.
[Condition of type UNDEFINED-FUNCTION]
<span class="inverse">R</span>estarts:
<span class="keyword">0:</span> [SKIP-GPG-CHECK] Don't check GPG signature for this package
<span class="keyword">1:</span> [RETRY] Retry SLIME REPL evaluation request.
<span class="keyword">2:</span> [*ABORT] Return to SLIME's top level.
<span class="keyword">3:</span> [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-thread" RUNNING {B0D7679}>)
Backtrace:
<span class="keyword">0:</span> ("bogus stack frame")
<span class="keyword">1:</span> (ASDF-INSTALL::VERIFY-GPG-SIGNATURE/STRING ..)
<span class="keyword">2:</span> (ASDF-INSTALL::VERIFY-GPG-SIGNATURE/URL "http://ftp.linux.org.uk/pub/lisp/...
<span class="keyword">3:</span> (ASDF-INSTALL::DOWNLOAD-FILES-FOR-PACKAGE "SPLIT-SEQUENCE" #P"/home/kovisoft/...
<span class="inverse">Slimv.SLDB [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^4,1^^^^^^^^^^^^Top</span>
</pre>
<p>Here we get an error because we don't have the public key for the package. For this tutorial we skip the GPG signature check, just as Marco did, but as he warns us we shouldn't.</p>
<p>So we select restart <span class="fixed">0: [SKIP-GPG-CHECK]</span> and wait for the installation of package <span class="fixed">split-sequence</span>:</p>
<pre class="repl-body">
<span class="prompt">CL-USER></span>
<span class="explain">;; Restart 0: [SKIP-GPG-CHECK] was selected here</span>
0
<span class="comment">; Evaluation aborted on NIL</span>
<span class="prompt">CL-USER></span> <span class="comment">; Quit to level 1</span>
<span class="prompt">CL-USER></span> Installing /home/kovisoft/SPLIT-SEQUENCE.asdf-install-...
split-sequence/README.cCLan-install
split-sequence/split-sequence.asd
split-sequence/split-sequence.lisp
<span class="comment">; loading system definition from
; /home/kovisoft/.sbcl/systems/split-sequence.asd into #<PACKAGE "ASDF0">
; registering #<SYSTEM :SPLIT-SEQUENCE {B957901}> as SPLIT-SEQUENCE
; compiling file "/home/kovisoft/.sbcl/site/split-sequence/split-sequence.lisp" ...
; compiling (DEFPACKAGE "SPLIT-SEQUENCE" ...)
; compiling (IN-PACKAGE "SPLIT-SEQUENCE")
; compiling (DEFUN SPLIT-SEQUENCE ...)
; compiling (DEFUN SPLIT-SEQUENCE-IF ...)
; compiling (DEFUN SPLIT-SEQUENCE-IF-NOT ...)
; compiling (DEFUN PARTITION ...)
; compiling (DEFUN PARTITION-IF ...)
; compiling (DEFUN PARTITION-IF-NOT ...)
; compiling (DEFINE-COMPILER-MACRO PARTITION ...)
; compiling (DEFINE-COMPILER-MACRO PARTITION-IF ...)
; compiling (DEFINE-COMPILER-MACRO PARTITION-IF-NOT ...)
; compiling (PUSHNEW :SPLIT-SEQUENCE ...)
; /home/kovisoft/.cache/common-lisp/sbcl-1.0.40.0.debian-linux-x86/home/kovisoft/...
; compilation finished in 0:00:00.657</span>
<span class="keyword">NIL</span>
</pre>
<p>Marco installed the <span class="fixed">split-sequence</span> package via <a href='http://common-lisp.net/project/asdf/'>ASDF</a>, but there is another way to do it using <a href='http://www.quicklisp.org'>Quicklisp</a>. You can install Quicklisp by loading <a href='http://beta.quicklisp.org/quicklisp.lisp'>quicklisp.lisp</a> into the REPL and evaluating <span class="fixed">(quicklisp-quickstart:install)</span>. If you want to start Quicklisp every time your Lisp starts, then evaluate <span class="fixed">(ql:add-to-init-file)</span>. You can perform the installation steps also from within Slimv:</p>
<pre class="repl-body">
<span class="prompt">CL-USER></span> <span class="paren1">(</span><span class="keyword">load</span> #P<span class="string">"/home/kovisoft/quicklisp.lisp"</span><span class="paren1">)</span>
==== quicklisp quickstart loaded ====
To continue, evaluate: (quicklisp-quickstart:install)
<span class="prompt">CL-USER></span> <span class="paren1">(</span>quicklisp-quickstart:install<span class="paren1">)</span>
<span class="comment">; Fetching #<URL "http://beta.quicklisp.org/quickstart/asdf.lisp">
; 159.59KB</span>
==================================================
163,424 bytes in 0.16 seconds (985.15KB/sec)
<span class="comment">; Fetching #<URL "http://beta.quicklisp.org/quickstart/quicklisp.tar">
; 180.00KB</span>
==================================================
184,320 bytes in 0.16 seconds (1097.56KB/sec)
<span class="comment">; Fetching #<URL "http://beta.quicklisp.org/quickstart/setup.lisp">
; 4.88KB</span>
==================================================
4,995 bytes in 0.001 seconds (4877.93KB/sec)
<span class="comment">; Upgrading ASDF from version 1.704 to version 2.014.6
; Fetching #<URL "http://beta.quicklisp.org/dist/quicklisp.txt">
; 0.40KB</span>
==================================================
408 bytes in 0.001 seconds (398.44KB/sec)
==== quicklisp installed ====
To load a system, use: (ql:quickload "system-name")
To find systems, use: (ql:system-apropos "term")
To load Quicklisp every time you start Lisp, use: (ql:add-to-init-file)
For more information, see http://www.quicklisp.org/beta/
<span class="keyword">NIL</span>
</pre>
<p>Assuming that you have already installed Quicklisp and it is running, this is how you install the <span class="fixed">split-sequence</span> package:</p>
<pre class="repl-body">
<span class="prompt">CL-USER></span> <span class="paren1">(</span>ql:quickload :split-sequence<span class="paren1">)</span>
To load <span class="string">"split-sequence"</span>:
Load <span class="number">1</span> ASDF system:
split-sequence
<span class="comment">; Loading "split-sequence"</span>
<span class="paren1">(</span>:SPLIT-SEQUENCE<span class="paren1">)</span>
</pre>
<h3 class='post-title entry-title'>
<a NAME="inspect">Inspecting a package
</h3>
<p>Let's verify that the newly installed package is available via <span class="fixed">find-package</span>. Inspect the package by pressing <span class="fixed">,i</span> (or by selecting Debugging/Inspect from the Slimv menu). We get a prompt to enter the symbol name to inspect. The prompt might be pre-filled with a symbol name under the cursor, but we ignore this and use <span class="fixed">*</span> (i.e. the result of the previous <span class="fixed">find-package</span> expression - the package object is this case).</p>
<p>The inspector has a hierarchical structure. You can inspect the n-th part below the current node by pressing <span class="fixed">Enter</span> in Normal mode on a line beginning with <span class="fixed">[nn]</span>, where <b>nn</b> is the part identifier. You can return to the previous node by pressing <span class="fixed">Enter</span> on the last line containing <span class="fixed">[<<]</span>.<br>
There may also be "actions" bound to the node, these are marked with <span class="fixed"><nn></span>, where <b>nn</b> is the action identifier. You also select actions by pressing <span class="fixed">Enter</span> on their line.</p>
<p>If we are new to a package, we can inspect the external symbols provided by the package. Then we can examine the function descriptions one by one:</p>
<pre class="repl-body">
<span class="prompt">CL-USER></span> <span class="paren1">(</span><span class="keyword">find-package</span> :split-sequence<span class="paren1">)</span>
#<<span class="keyword">PACKAGE</span> <span class="string">"SPLIT-SEQUENCE"</span>>
<span class="prompt">CL-USER></span>
<span class="explain">;; "Inspect: *" was used here</span>
<span class="inverse">Slimv.REPL.lisp 112,0-1 All</span>
Inspecting #<PACKAGE {B99A001}>
--------------------
[1] Name: "SPLIT-SEQUENCE"
[2] Nick names: "PARTITION"
[3] Use list: COMMON-LISP
Used by list:
[4] 35 present symbols.
<span class="inverse">[</span>5] 6 external symbols.
[6] 29 internal symbols.
[7] 978 inherited symbols.
0 shadowed symbols.
[<<]
<span class="explain">;; Item [5] was selected here</span>
<span class="inverse">Slimv.INSPECT [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^8,1^^^^^^^^^^^^Top</span>
Inspecting #<SWANK::%PACKAGE-SYMBOLS-CONTAINER {AF5EC21}>
--------------------
All external symbols of package "SPLIT-SEQUENCE"
A symbol is considered external of a package if it's
"part of the `external interface' to the package and
[is] inherited by any other package that uses the
package." (CLHS glossary entry of `external')
<0> [Group by classification]
Symbols: Flags:
------------------------------ --------
[1] PARTITION -f-----p
[2] PARTITION-IF -f------
[3] PARTITION-IF-NOT -f------
<span class="inverse">[</span>4] SPLIT-SEQUENCE -f-----p
[5] SPLIT-SEQUENCE-IF -f------
[6] SPLIT-SEQUENCE-IF-NOT -f------
[<<]
<span class="explain">;; Item [4] was selected here</span>
<span class="inverse">Slimv.INSPECT [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^17,1^^^^^^^^^^^^Top</span>
Inspecting #<SYMBOL {B99A1AF}>
--------------------
[1] Its name is: "SPLIT-SEQUENCE"
It is unbound.
[2] It is a function: #<FUNCTION SPLIT-SEQUENCE:SPLIT-SEQUENCE>
<0> [unbind]
Function documentation:
Return a list of subsequences in seq delimited by delimiter.
If :remove-empty-subseqs is NIL, empty subsequences will be included
in the result; otherwise they will be discarded. All other keywords
work analogously to those for CL:SUBSTITUTE. In particular, the
behaviour of :from-end is possibly different from other versions of
this function; :from-end values of NIL and T are equivalent unless
:count is supplied. The second return value is an index suitable as an
argument to CL:SUBSEQ into the sequence indicating where processing
stopped.
[3] It is external to the package: SPLIT-SEQUENCE
<1> [unintern]
[4] Property list: NIL
[5] It names the package: @0=#<PACKAGE "SPLIT-SEQUENCE">
[<<]
<span class="inverse">Slimv.INSPECT [RO]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^1,1^^^^^^^^^^^^Top</span>
</pre>
<p><i>In the above example we selected the same items in the inspector as Marco did in the movie. Our actions are marked by green lines, these are explanatory messages, they are normally not printed by the REPL.</i></p>
<p>Now package <span class="fixed">split-sequence</span> is installed and ready for use in <span class="fixed">morse-to-string</span>.</p>
<br>
<span class='nav-prev'>Previous: <a href='tutorial.html'>Part One</a></span>
<span class='nav-next'>Next: <a href='tutorial3.html'>Part Three</a></span>
<br>
<br>
<hr />
<p><i>Written by <a href='http://www.vim.org/account/profile.php?user_id=16470'>Tamas Kovacs</a><br>
Last updated on Aug 28, 2020</i></p>
<br>
</div>
<div id="sidebar">
<h3 class='post-title entry-title'>
Contents
</h3>
<p><b><a href='tutorial.html'>Part One</a></b></p>
<a href='tutorial.html#downloading'>Downloading</a><br>
<a href='tutorial.html#configuration'>Configuration</a><br>
<a href='tutorial.html#startup'>Starting up</a><br>
<a href='tutorial.html#basic'>Basic REPL operations</a><br>
<a href='tutorial.html#edit'>Editing a source file</a><br>
<p><b>Part Two</b><br></p>
<a href='#sldb'>Using the SLIME debugger</a><br>
<a href='#debug'>More debugging methods</a><br>
<a href='#asdf'>Installing a package</a><br>
<a href='#inspect'>Inspecting a package</a><br>
<p><b><a href='tutorial3.html'>Part Three</a></b></p>
<a href='tutorial3.html#trace'>Tracing</a><br />
<a href='tutorial3.html#inspobj'>Inspecting objects</a><br />
<a href='tutorial3.html#xref'>Cross reference</a><br />
</div>
</body>
</html>
<!--
vim:wrap:
-->