-
Notifications
You must be signed in to change notification settings - Fork 7
/
s7.html
9696 lines (8087 loc) · 310 KB
/
s7.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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<!-- documentation for s7 -->
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>s7</title>
<style type="text/css">
EM.red {color:red; font-style:normal}
EM.normal {font-style:normal}
EM.redb {color:red; font-weight: bold; font-style: normal}
EM.error {color:chocolate; font-style:normal}
EM.emdef {font-weight: bold; font-style: normal}
EM.green {color:green; font-style:normal}
EM.gray {color:#505050; font-style:normal}
EM.big {font-size: 20px;
font-style: normal;
}
EM.bigger {font-size: 30px;
font-style: normal;
}
EM.def {font-style: normal}
H1 {text-align: center}
UL {list-style-type: none}
A {text-decoration:none}
A:hover {text-decoration:underline}
A.def {font-weight: bold;
font-style: normal;
text-decoration:none;
text-color:black;
}
PRE.indented {padding-left: 1.0cm;}
DIV.indented {background-color: #F8F8F0;
padding-left: 0.5cm;
padding-right: 0.5cm;
padding-top: 0.5cm;
padding-bottom: 0.5cm;
margin-bottom: 0.5cm;
border: 1px solid gray;
border-radius: 20px;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
}
DIV.header {margin-top: 60px;
margin-bottom: 30px;
border: 4px solid #00ff00; /* green */
background-color: #eefdee; /* lightgreen */
padding-left: 30px;
}
DIV.topheader {margin-top: 10px;
margin-bottom: 40px;
border: 4px solid #00ff00; /* green */
background-color: #f5f5dc; /* beige */
font-family: 'Helvetica';
font-size: 30px;
text-align: center;
padding-top: 10px;
padding-bottom: 10px;
}
DIV.separator {margin-top: 30px;
margin-bottom: 10px;
border: 2px solid #00ff00; /* green */
background-color: #f5f5dc; /* beige */
padding-top: 4px;
width: 30%;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
DIV.bluishbordered {background-color: #f2f4ff;
border: 1px solid #000000;
padding-left: 10px;
padding-right: 10px;
padding-top: 10px;
padding-bottom: 10px;
}
DIV.brownishbordered {background-color: #fbfbf0;
border: 1px solid #000000;
padding-left: 10px;
padding-right: 10px;
padding-top: 10px;
padding-bottom: 10px;
}
BODY.body {background-color: #ffffff; /* white */
margin-right: 20px;
margin-left: 20px;
}
DIV.orange {background-color: #ffa500; /* orange */
padding-left: 6px;
padding-right: 6px;
padding-top: 4px;
padding-bottom: 4px;
border: 4px solid #000000;
font-family: 'Helvetica';
font-size: 12px;
font-weight: bold;
text-align: center;
margin-left: 0.5cm;
margin-right: 1.0cm;
float: right;
border-radius: 8px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
}
DIV.listener {background-color: #f0f8ff;
font-family: 'Monospace';
padding-left: 6px;
padding-right: 6px;
padding-bottom: 4px;
margin-left: 1.0cm;
margin-right: 4.0cm;
border: 2px solid #a0a0a0;
}
LI.li_header {padding-top: 20px;}
SUMMARY.indented {background-color: #F8F8F0;
padding-top: 8px;
padding-bottom: 8px;
margin-bottom: 0.5cm;
border: 1px solid gray;
text-align: center;
width: 30%;
border-radius: 8px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
}
</style>
</head>
<body class="body">
<!-- INDEX s7doc:s7 scheme -->
<div class="topheader" id="s7doc">s7
</div>
<p>s7 is a Scheme implementation intended as an extension language
for other applications, primarily Snd, Radium, and Common Music. It exists as just two files, s7.c and
s7.h, that want only to disappear into someone else's source tree. There are no libraries,
no run-time init files, and no configuration scripts.
It can be built as a stand-alone
interpreter (see <a href="#repl">below</a>). s7test.scm is a regression test for s7.
A tarball is available: <a href="ftp://ccrma-ftp.stanford.edu/pub/Lisp/s7.tar.gz">s7 tarball</a>.
</p>
<p>
s7 is the default extension language of Snd and sndlib (<a href="http://ccrma.stanford.edu/software/snd/index.html">snd</a>),
Rick Taube's Common Music (commonmusic at sourceforge), and Kjetil Matheussen's Radium music editor.
There are X, Motif, Gtk, and openGL bindings
in libxm in the Snd tarball, or at ftp://ccrma-ftp.stanford.edu/pub/Lisp/libxm.tar.gz.
If you're running s7 in a context
that has getenv, file-exists?, and system, you can use s7-slib-init.scm
to gain easy access to slib. This init file is named "s7.init" in the slib distribution.
</p>
<p>Although it is a descendant of tinyScheme, s7 is closest as a Scheme dialect to Guile 1.8.
I believe it is compatible with <a href="#s7vsr5rs">r5rs</a> and <a href="#r7rs">r7rs</a>: you can just ignore all the additions discussed in this file.
It has continuations,
ratios, complex numbers,
macros, keywords, hash-tables,
multiprecision arithmetic,
generalized set!, unicode, and so on.
It does not have syntax-rules or any of
its friends, and it does not think there is any such thing
as an inexact integer.
</p>
<p>This file assumes you know about Scheme and all its problems,
and want a quick tour of where s7 is different. (Well, it was quick once upon a time).
The main difference: if it's in s7, it's a first-class citizen of s7, and that includes
macros, environments, and syntactic values.
</p>
<br>
<blockquote>
<div class="indented">
<p>I originally used a small font for scholia, but now I have to squint
to read that tiny text, so less-than-vital commentaries are shown in the normal font, but
indented and on a sort of brownish background.
</p>
</div>
</blockquote>
<br>
<!--
<div class="orange">
<p>Danger!</p>
<p>Men Working</p>
<p>in Trees</p>
</div>
-->
<ul>
<li><a href="#multiprecision">arbitrary precision arithmetic</a>
<li><a href="#math">math functions</a>
<li><a href="#define*">define*, named let*</a>
<li><a href="#macros">define-macro</a>
<li><a href="#pws">procedure-setter</a>
<li><a href="#generalizedset">generalized set!</a>
<li><a href="#multidimensionalvectors">multidimensional vectors</a>
<li><a href="#hashtables">hash tables</a>
<li><a href="#environments">environments</a>
<li><a href="#multiplevalues">multiple-values</a>
<li><a href="#callwithexit1">call-with-exit</a>
<li><a href="#format1">format</a>
<li><a href="#hooks">hooks</a>
<li><a href="#procedureinfo">procedure info</a>
<li><a href="#evalstring">eval</a>
<li><a href="#IO">IO and other OS functions</a>
<li><a href="#errors">errors</a>
<li><a href="#autoload">autoload</a>
<li><a href="#constants">define-constant, symbol-access</a>
<li><a href="#miscellanea">marvels and curiousities:</a>
<ul>
<li><a href="#loadpath">*load-path*</a>, <a href="#featureslist">*features*</a>, <a href="#sharpreaders">*#readers*</a>,
<li><a href="#makelist">make-list</a>, <a href="#charposition">char-position</a>, <a href="#keywords">keywords</a>
<li><a href="#symboltable">symbol-table</a>, <a href="#s7help">help</a>, <a href="#s7gc">gc</a>, <a href="#morallyequalp">morally-equal?</a>
<li><a href="#expansion">define-expansion</a>, <a href="#s7env">*s7*</a>, <a href="#s7vsr5rs">r5rs</a>, <a href="#r7rs">r7rs</a>,
<li><a href="#profiling">profiling</a>, <a href="#circle">circular lists</a>, <a href="#legolambda">legolambda</a>, etc...
</ul>
<li class="li_header"><a href="#FFIexamples">FFI examples</a>
<ul>
<li><a href="#repl">read-eval-print loop (and emacs)</a>
<li><a href="#defun">define a function with arguments and a returned value, and define a variable </a>
<li><a href="#defvar">call a Scheme function from C, and get/set Scheme variable values in C</a>
<li><a href="#juce">C++ and Juce</a>
<li><a href="#sndlib">load sndlib using the Xen functions and macros</a>
<li><a href="#pwstype">add a new Scheme type and a procedure with a setter</a>
<li><a href="#functionportexample">redirect display output to a C procedure</a>
<li><a href="#extendop">extend a built-in operator ("+" in this case)</a>
<li><a href="#definestar1">C-side define* (s7_define_function_star)</a>
<li><a href="#definemacro1">C-side define-macro (s7_define_macro)</a>
<li><a href="#definegeneric">define a generic function in C</a>
<li><a href="#signal">signal handling (C-C to break out of an infinite loop)</a>
<li><a href="#vector">direct multidimensional vector element access</a>
<li><a href="#notify">notification in C that a Scheme variable has been set!</a>
<li><a href="#namespace">Load C defined stuff into a separate namespace</a>
<li><a href="#Cerrors">Error handling in C</a>
<li><a href="#testhook">Hooks in C and Scheme</a>
<li><a href="#dload">Load a C module dynamically</a>
<li><a href="#gmpex">gmp and friends</a>
<li><a href="#glistener">glistener.c</a>
<li><a href="#gdb">gdb</a>
</ul>
<li class="li_header"><a href="#s7examples">s7 examples</a>
<ul>
<li><a href="#cload">cload.scm</a>
<ul>
<li><a href="#libc">libc</a>
<li><a href="#libgsl">libgsl</a>
<li><a href="#libgdbm">libgdbm</a>
</ul>
<li><a href="#schemerepl">repl.scm</a>
<li><a href="#lint">lint.scm</a>
</ul>
</ul>
<div class="header" id="multiprecision"><h4>multiprecision arithmetic</h4></div>
<p>All numeric types, integers, ratios, reals, and complex numbers are supported.
The basic integer and real
types are defined in s7.h, defaulting to long long int and double.
A ratio consists of two integers, a complex number two reals.
pi is predefined, as are
most-positive-fixnum and most-negative-fixnum.
s7 can be built with multiprecision support
for all types, using the gmp, mpfr, and mpc libraries (set WITH_GMP to 1 in s7.c).
If multiprecision arithmetic is
enabled, the following functions are included: bignum, and bignum?, and the variable (*s7* 'bignum-precision).
(*s7* 'bignum-precision) defaults to 128; it sets the number of bits each float takes.
pi automatically reflects the current (*s7* 'bignum-precision):
</p>
<pre class="indented">
> pi
<em class="gray">3.141592653589793238462643383279502884195E0</em>
> (*s7* 'bignum-precision)
<em class="gray">128</em>
> (set! (*s7* 'bignum-precision) 256)
<em class="gray">256</em>
> pi
<em class="gray">3.141592653589793238462643383279502884197169399375105820974944592307816406286198E0</em>
</pre>
<p>
<em class=def id="bignump">bignum?</em> returns #t if its argument is a big number of some type; I use "bignum"
for any big number, not just integers. To create a big number,
either include enough digits to overflow the default types, or use the <em class=def id="bignum">bignum</em> function.
Its argument is a string representing the desired number:
</p>
<pre class="indented">
> (bignum "123456789123456789")
<em class="gray">123456789123456789</em>
> (bignum "1.123123123123123123123123123")
<em class="gray">1.12312312312312312312312312300000000009E0</em>
</pre>
<blockquote>
<div class="indented">
<p>In the non-gmp case, if s7 is built using doubles (s7_double in s7.h), the float "epsilon" is
around (expt 2 -53), or about 1e-16. In the gmp case, it is around (expt 2 (- (*s7* 'bignum-precision))).
So in the default case (precision = 128), using gmp:
</p>
<pre class="indented">
> (= 1.0 (+ 1.0 (expt 2.0 -128)))
<em class="gray">#t</em>
> (= 1.0 (+ 1.0 (expt 2.0 -127)))
<em class="gray">#f</em>
</pre>
<p>and in the non-gmp case:
</p>
<pre class="indented">
> (= 1.0 (+ 1.0 (expt 2 -53)))
<em class="gray">#t</em>
> (= 1.0 (+ 1.0 (expt 2 -52)))
<em class="gray">#f</em>
</pre>
<p>In the gmp case, integers and ratios are limited only by the size of memory,
but reals are limited by (*s7* 'bignum-precision). This means, for example, that
</p>
<pre class="indented">
> (floor 1e56) ; (*s7* 'bignum-precision) is 128
<em class="gray">99999999999999999999999999999999999999927942405962072064</em>
> (set! (*s7* 'bignum-precision) 256)
<em class="gray">256</em>
> (floor 1e56)
<em class="gray">100000000000000000000000000000000000000000000000000000000</em>
</pre>
<p>The non-gmp case is similar, but it's easy to find the edge cases:
</p>
<pre class="indented">
> (floor (+ 0.9999999995 (expt 2.0 23)))
<em class="gray">8388609</em>
</pre>
</div>
</blockquote>
<div class="header" id="math"><h4>math functions</h4></div>
<p>
s7 includes:
</p>
<ul>
<li>sinh, cosh, tanh, asinh, acosh, atanh
<li>logior, logxor, logand, lognot, logbit?, ash, integer-length, integer-decode-float
<li>random
<li>nan?, infinite?
</ul>
<p>
The random function can take any numeric argument, including 0.
The following constants are predefined: pi, most-positive-fixnum, most-negative-fixnum.
Other math-related differences between s7 and r5rs:
</p>
<ul>
<li>rational? and exact mean integer or ratio (i.e. not floating point), inexact means not exact.
<li>floor, ceiling, truncate, and round return (exact) integer results.
<li>"#" does not stand for an unknown digit.
<li>the "@" complex number notation is not supported ("@" is an exponent marker in s7).
<li>"+i" is not considered a number; include the real part.
<li>modulo, remainder, and quotient take integer, ratio, or real arguments.
<li>lcm and gcd can take integer or ratio arguments.
<li>log takes an optional second argument, the base.
<li>'.' and an exponent can occur in a number in any base.
<li>rationalize returns a ratio!
<li>case is significant in numbers, as elsewhere: #b0 is 0, but #B0 is an error.
</ul>
<pre class="indented">
> (exact? 1.0)
<em class="gray">#f</em>
> (rational? 1.5)
<em class="gray">#f</em>
> (floor 1.4)
<em class="gray">1</em>
> (remainder 2.4 1)
<em class="gray">0.4</em>
> (modulo 1.4 1.0)
<em class="gray">0.4</em>
> (lcm 3/4 1/6)
<em class="gray">3/2</em>
> (log 8 2)
<em class="gray">3</em>
> (number->string 0.5 2)
<em class="gray">"0.1"</em>
> (string->number "0.1" 2)
<em class="gray">0.5</em>
> (rationalize 1.5)
<em class="gray">3/2</em>
> (complex 1/2 0)
<em class="gray">1/2</em>
> (logbit? 6 1) ; argument order, (logbit? int index), follows gmp, not CL
<em class="gray">#t</em>
</pre>
<p>See <a href="#libgsl">cload and libgsl.scm</a> for easy access to GSL,
and similarly libm.scm for the C math library.
</p>
<blockquote>
<div class="indented">
<p>The exponent itself is always in base 10; this follows gmp usage.
Scheme normally uses "@" for its useless polar notation, but that
means <code>(string->number "1e1" 16)</code> is ambiguous — is the "e" a digit or an exponent marker?
In s7, "@" is an exponent marker.
</p>
<pre class="indented">
> (string->number "1e9" 2) ; (expt 2 9)
<em class="gray">512.0</em>
> (string->number "1e1" 12) ; "e" is not a digit in base 12
<em class="gray">#f</em>
> (string->number "1e1" 16) ; (+ (* 1 16 16) (* 14 16) 1)
<em class="gray">481</em>
> (string->number "1.2e1" 3); (* 3 (+ 1 2/3))
<em class="gray">5.0</em>
</pre>
</div>
<div class="indented">
<p>Should s7 predefine the numbers +inf.0, -inf.0, and nan.0? It doesn't currently, but you can
get them via log or 1/0 (see <a href="#r7rs">below</a>).
But what is <code>(/ 1.0 0.0)</code>? s7 gives a "division by zero" error here, and also in <code>(/ 1 0)</code>.
Guile returns +inf.0 in the first case, which seems reasonable, but a "numerical overflow" error in the second.
Slightly weirder is <code>(expt 0.0 0+i)</code>. Currently s7 returns 0.0, Guile returns +nan.0+nan.0i,
Clisp and sbcl throw an error. Everybody agrees that <code>(expt 0 0)</code> is 1, and Guile thinks
that <code>(expt 0.0 0.0)</code> is 1.0. But <code>(expt 0 0.0)</code> and <code>(expt 0.0 0)</code> return different
results in Guile (1 and 1.0), both are 0.0 in s7, the first is an error in Clisp, but the second returns 1,
and so on — what a mess! This mess was made a lot worse than it needs to be when the IEEE decreed that
0.0 equals -0.0, so we can't tell them apart, but that they produce different results in nearly every use!
</p>
<pre class="indented">
scheme@(guile-user)> (= -0.0 0.0)
<em class="gray">#t</em>
scheme@(guile-user)> (negative? -0.0)
<em class="gray">#f</em>
scheme@(guile-user)> (= (/ 1.0 0.0) (/ 1.0 -0.0))
<em class="gray">#f</em>
scheme@(guile-user)> (< (/ 1.0 -0.0) -1e100 1e100 (/ 1.0 0.0))
<em class="gray">#t</em>
</pre>
<p>
How can they be equal? In s7, the sign
of -0.0 is ignored, and they really are equal.
One other oddity: two floats can satisfy eq? and yet not be eqv?:
<code>(eq? nan.0 nan.0)</code> might be #t (it is unspecified), but <code>(eqv? nan.0 nan.0)</code> is #f.
The same problem afflicts memq and assq.
</p>
</div>
<div class="indented">
<p>The <em class=def id="random">random</em> function takes a range and an optional state, and returns a number
between zero and the range, of the same type as the range. It is perfectly reasonable
to use a range of 0, in which case random returns 0.
<em class=def id="randomstate">random-state</em> creates a new random state from a seed. If no state is passed,
random uses some default state initialized from the current time. <em class=def id="randomstatep">random-state?</em> returns #t if passed a random state object.
</p>
<pre class="indented">
> (random 0)
<em class="gray">0</em>
> (random 1.0)
<em class="gray">0.86331198514245</em>
> (random 3/4)
<em class="gray">654/1129</em>
> (random 1+i)
<em class="gray">0.86300308872748+0.83601002730848i</em>
> (random -1.0)
<em class="gray">-0.037691127513267</em>
> (define r0 (random-state 1234))
<em class="gray">r0</em>
> (random 100 r0)
<em class="gray">94</em>
> (random 100 r0)
<em class="gray">19</em>
> (define r1 (random-state 1234))
<em class="gray">r1</em>
> (random 100 r1)
<em class="gray">94</em>
> (random 100 r1)
<em class="gray">19</em>
</pre>
<p>copy the random-state to save a spot in a random number sequence, or save the random-state as a list via
random-state->list, then to restart from that point, apply random-state to that list.
</p>
</div>
<div class="indented">
<p>I can't find the right tone for this section; this is the 400-th revision; I wish I were a better writer!
</p>
<p>In some Schemes,
"rational" means "could possibly be
expressed equally well as a ratio: floats are approximations". In s7 it's: "is actually expressed (at the scheme level) as a ratio (or an integer of course)";
otherwise "rational?" is the same as "real?":
</p>
<pre class="indented">
(not-s7)> (rational? (sqrt 2))
<em class="gray">#t</em>
</pre>
<p>That 1.0 is represented at the IEEE-float level as a sort of
ratio does not mean it has to be a scheme ratio; the two notions are independent.
</p>
<p>But that confusion is trivial compared to the completely nutty "inexact integer".
As I understand it, "inexact" originally meant "floating point", and "exact" meant integer or ratio of integers.
But words have a life of their own.
0.0 somehow became an "inexact" integer (although it can be represented exactly in floating
point).
+inf.0 must be an integer —
its fractional part is explicitly zero! But +nan.0...
And then there's:
</p>
<pre class="indented">
(not-s7)> (integer? 9007199254740993.1)
<em class="gray">#t</em>
</pre>
<p>
When does this matter? I often need to index into a vector, but the index is a float (a "real" in Scheme-speak: its
fractional part can be non-zero).
In one Scheme:
</p>
<pre class="indented">
(not-s7)> (vector-ref #(0) (floor 0.1))
<em class="gray">ERROR: Wrong type (expecting exact integer): 0.0 </em>; [why? "it's probably a programmer mistake"!]
</pre>
<p>Not to worry, I'll use inexact->exact:
</p>
<pre class="indented">
(not-s7)> (inexact->exact 0.1)
<em class="gray">3602879701896397/36028797018963968 </em>; [why? "floats are ratios"!]
</pre>
<p>So I end up using the verbose <code>(floor (inexact->exact ...))</code> everywhere, and even
then I have no guarantee that I'll get a legal vector index.
I have never seen any use made of the exact/inexact distinction — just
wild flailing to try get around it.
I think the whole idea is confused and useless, and leads
to verbose and buggy code.
If we discard it,
we can maintain backwards compatibility via:
</p>
<pre class="indented">
(define exact? rational?)
(define (inexact? x) (not (rational? x)))
(define inexact->exact rationalize) ; or floor
(define (exact->inexact x) (* x 1.0))
</pre>
<p>#i and #e are also useless because you can
have any number after, for example, #b:
</p>
<pre class="indented">
> #b1.1
<em class="gray">1.5</em>
> #b1e2
<em class="gray">4.0</em>
> #o17.5+i
<em class="gray">15.625+1i</em>
</pre>
<p>Speaking of #b and friends, what should <code>(string->number "#xffff" 2)</code> return?
</p>
</div>
<details>
<summary class="indented">more examples</summary>
<div class="indented">
<pre>
(define (log-n-of n . ints) ; return the bits on in exactly n of ints
(let ((len (length ints)))
(cond ((= len 0) (if (= n 0) -1 0))
((= n 0) (lognot (apply logior ints)))
((= n len) (apply logand ints))
((> n len) 0)
(#t
(do ((1s 0)
(prev ints)
(i 0 (+ i 1)))
((= i len) 1s)
(let ((cur (ints i)))
(if (= i 0)
(set! 1s (logior 1s (logand cur (apply log-n-of (- n 1) (cdr ints)))))
(let ((mid (cdr prev)))
(set! (cdr prev) (if (= i (- len 1)) () (cdr mid)))
(set! 1s (logior 1s (logand cur (apply log-n-of (- n 1) ints))))
(set! (cdr prev) mid)
(set! prev mid)))))))))
</pre>
</div>
</details>
</blockquote>
<div class="header" id="define*"><h4>define*, lambda*</h4></div>
<p><em class=def id="definestar">define*</em> and
<em class=def id="lambdastar">lambda*</em>
are extensions of define and lambda that make it easier
to deal with optional, keyword, and rest arguments.
The syntax is very simple: every argument to define* has a default value
and is automatically available as a keyword argument. The default value
is either #f if unspecified, or given in a list whose first member is
the argument name.
The last argument
can be preceded by :rest or a dot to indicate that all other trailing arguments
should be packaged as a list under that argument's name. A trailing or rest
argument's default value is ().
</p>
<pre class="indented">
(<em class=red>define*</em> (hi a (b 32) (c "hi")) (list a b c))
</pre>
<p>Here the argument "a" defaults to #f, "b" to 32, etc.
When the function is called,
the argument names are set from the values passed the function,
then any unset arguments are bound to their default values, evaluated in left-to-right order.
As the current argument list is scanned, any name that occurs as a keyword, :arg for example where the parameter name is arg,
sets that argument's new value. Otherwise, as values occur, they
are plugged into the actual argument list based on their position, counting a keyword/value pair as one argument.
This is called an optional-key list in CLM. So, taking the function
above as an example:
</p>
<pre class="indented">
> (hi 1)
<em class="gray">(1 32 "hi")</em>
> (hi :b 2 :a 3)
<em class="gray">(3 2 "hi")</em>
> (hi 3 2 1)
<em class="gray">(3 2 1)</em>
</pre>
<p>See s7test.scm for many examples. (s7's define* is very close to srfi-89's define*).
</p>
<blockquote>
<div class="indented">
<p>The combination of optional and keyword arguments is viewed with disfavor in the Lisp
community, but the problem is in CL's implementation of the idea, not the idea itself.
I've used the s7 style since around 1976, and have never found it confusing. The mistake
in CL is to require the optional arguments if a keyword argument occurs, and to consider them as distinct from the
keyword arguments. So everyone forgets and puts a keyword where CL expects a required-optional
argument. CL then does something ridiculous, and the programmer stomps around shouting about keywords, but the fault lies with CL.
If s7's way is considered too loose, one way to tighten it might be to insist that once a keyword
is used, only keyword argument pairs can follow.
</p>
</div>
<div class="indented">
<p>A natural companion of lambda* is named let*. In named let, the implicit function's
arguments have initial values, but thereafter, each call requires the full set of arguments.
Why not treat the initial values as default values?
</p>
<pre class="indented">
> (let* func ((i 1) (j 2))
(+ i j (if (> i 0) (func (- i 1)) 0)))
<em class="gray">5</em>
> (letrec ((func (lambda* ((i 1) (j 2))
(+ i j (if (> i 0) (func (- i 1)) 0)))))
(func))
<em class="gray">5</em>
</pre>
<p>This is consistent with the lambda* arguments because their defaults are
already set in left-to-right order, and as each parameter is set to its default value,
the binding is added to the default value expression environment (just as if it were a let*).
The let* name itself (the implicit function) is not defined until after the bindings
have been evaluated (as in named let).
</p>
<p>In CL, keyword default values are handled in the same way:
</p>
<pre class="indented">
> (defun foo (&key (a 0) (b (+ a 4)) (c (+ a 7))) (list a b c))
<em class="gray">FOO </em>
> (foo :b 2 :a 60)
<em class="gray">(60 2 67) </em>
</pre>
<p>In s7, we'd use:
</p>
<pre class="indented">
(define* (foo (a 0) (b (+ a 4)) (c (+ a 7))) (list a b c))
</pre>
</div>
<div class="indented">
<p>To try to catch what I believe are usually mistakes, I added two
error checks. One is triggered if you set the same parameter twice
in the same call, and the other if an unknown keyword is encountered
in the key position. These problems arise in a case such as
</p>
<pre class="indented">
(define* (f (a 1) (b 2)) (list a b))
</pre>
<p>You could do any of the following by accident:
</p>
<pre class="indented">
(f 1 :a 2) ; what is a?
(f :b 1 2) ; what is b?
(f :c 3) ; did you really want a to be :c and b to be 3?
</pre>
<p>In the last case, to pass a keyword deliberately, either include the
argument keyword: <code>(f :a :c)</code>, or make the default value a keyword:
<code>(define* (f (a :c) ...))</code>.
To turn off this error check, add :allow-other-keys at the end of the parameter list.
</p>
</div>
<details>
<summary class="indented">rest arg nits</summary>
<div class="indented">
<p>s7's lambda* arglist handling is not the same as CL's lambda-list. First,
you can have more than one :rest parameter:
</p>
<pre class="indented">
> ((lambda* (:rest a :rest b) (map + a b)) 1 2 3 4 5)
<em class="gray">'(3 5 7 9)</em>
</pre>
<p>and second, the rest parameter, if any, takes up an argument slot just like any other
argument:
</p>
<pre class="indented">
> ((lambda* ((b 3) :rest x (c 1)) (list b c x)) 32)
<em class="gray">(32 1 ())</em>
> ((lambda* ((b 3) :rest x (c 1)) (list b c x)) 1 2 3 4 5)
<em class="gray">(1 3 (2 3 4 5))</em>
</pre>
<p>CL would agree with the first case if we used &key for 'c', but would give an error in the second.
Of course, the major difference is that s7 keyword arguments don't insist that the key be present.
The :rest argument is needed in cases like these because we can't use an expression
such as:
</p>
<pre class="indented">
> ((lambda* ((a 3) . b c) (list a b c)) 1 2 3 4 5)
<em class="red">error: </em><em class="gray">stray dot?</em>
> ((lambda* (a . (b 1)) b) 1 2) ; the reader turns the arglist into (a b 1)
<em class="red">error: </em><em class="gray">lambda* parameter '1 is a constant</em>
</pre>
<p>Yet another nit: the :rest argument is not considered a keyword argument, so
</p>
<pre class="indented">
> (define* (f :rest a) a)
<em class="gray">f</em>
> (f :a 1)
<em class="gray">(:a 1)</em>
</pre>
</div>
</details>
<!--
unknown key handling is not good, :allow-key-values?
(define* (f1 a) (list a))
(f1 :hi) -> (:hi)
(f1 ':hi) -> (:hi)
(f1 :a :hi) -> (:hi)
(define* (f2 a :allow-other-keys) (list a))
(f2 :hi) -> (:hi)
(define* (f3 a b :allow-other-keys) (list a b))
(f3 :hi 0) -> (#f #f)
(f3 :hi) => (:hi #f)
(define* (f4 a) (list (symbol->keyword a))) ; or string->keyword
(f4 'hi) -> (:hi)
(define* (f5 a)
((lambda* (hi) (list hi)) (symbol->keyword a) 32))
(f5 'hi) -> (32)
(define* (f6 a b)
((lambda* (hi) (list hi)) a b))
(f6 :hi 1) -> unknown key :hi
(define* (f7 a b) (list a b))
(f7 :hi) -> (:hi #f)
(f7 0 :hi) -> (0 :hi)
(f7 :hi 0) -> unknown key
(f7 :a :hi) -> (:hi #f)
(f7 :a :hi 32) -> (:hi 32)
(define* (f8 a b)
((lambda* (hi ho) (list hi ho)) (symbol->keyword a) b))
(f8 'hi 32) -> (32 #f)
(f8 'ho 32) -> (#f 32)
-->
</blockquote>
<div class="header" id="macros"><h4>macros</h4></div>
<p><em class=def id="definemacro">define-macro</em>,
<em class=def id="definemacrostar">define-macro*</em>,
<em class=def id="macroexpand">macroexpand</em>,
<em class=def id="gensym">gensym</em>,
<em class=def id="gensym?">gensym?</em>, and
<em class=def id="macrop">macro?</em>
implement the standard old-time macros.
</p>
<pre class="indented">
> (define-macro (and-let* vars . body)
`(let () (and ,@(map (lambda (v) `(define ,@v)) vars) (begin ,@body))))
> (define-macro (<em class=def id="trace">trace</em> f)
`(define ,f
(apply lambda 'args
`((format () "(~A ~{~A~^ ~}) -> " ',',f args)
(let ((val (apply ,,f args)))
(format () "~A~%" val)
val)))))
<em class="gray">trace</em>
> (trace abs)
<em class="gray">abs</em>
> (abs -1.5)
<em class="gray">(abs -1.5) -> 1.5</em>
<em class="gray">1.5</em>
</pre>
<p>macroexpand can help debug a macro. I always forget that it
wants an expression:
</p>
<pre class="indented">
> (define-macro (add-1 arg) `(+ 1 ,arg))
<em class="gray">add-1</em>
> (macroexpand (add-1 32))
<em class="gray">(+ 1 32)</em>
</pre>
<p>gensym returns a symbol that is guaranteed to be unused. It takes an optional string argument
giving the new symbol name's prefix. gensym? returns #t if its argument is a symbol created by gensym.
</p>
<pre class="indented">
(define-macro (pop! sym)
(let ((v (<em class=red>gensym</em>)))
`(let ((,v (car ,sym)))
(set! ,sym (cdr ,sym))
,v)))
</pre>
<p>As in define*, the starred forms give optional and keyword arguments:
</p>
<pre class="indented">
> (define-macro* (add-2 a (b 2)) `(+ ,a ,b))
<em class="gray">add-2</em>
> (add-2 1 3)
<em class="gray">4</em>
> (add-2 1)
<em class="gray">3</em>
> (add-2 :b 3 :a 1)
<em class="gray">4</em>
</pre>
<p>A bacro is a macro that expands its body and evaluates
the result in the calling environment.
</p>
<pre class="indented">
(define setf
(let ((args (gensym))
(name (gensym)))
(apply <em class=red>define-bacro</em> `((,name . ,args)
(unless (null? ,args)
(apply set! (car ,args) (cadr ,args) ())
(apply setf (cddr ,args)))))))
</pre>
<p>
The setf argument is a gensym (created when setf is defined) so that its name does not shadow any existing
variable. Bacros expand in the calling environment, and a normal argument name
might shadow something in that environment while the bacro is being expanded.
Similarly, if you introduce bindings in the bacro expansion code, you need to
keep track of which environment you want things to happen in. Use with-let
and gensym liberally.
stuff.scm has bacro-shaker which can find inadvertent name collisions,
but it is flighty and easily confused.
See s7test.scm for many examples of macros including such perennial favorites as
loop, dotimes, do*, enum, pushnew, and defstruct.
The calling environment itself is (outlet (curlet)) from within a bacro, so
</p>
<pre class="indented">
(define-bacro (holler)
`(format *stderr* "(~S~{ ~S ~S~^~})~%"
(let ((f __func__))
(if (pair? f) (car f) f))
(map (lambda (slot)
(values (symbol->keyword (car slot)) (cdr slot)))
(reverse (map values ,(outlet (curlet)))))))
(define (f1 a b)
(holler)
(+ a b))
(f1 2 3) ; prints out "(f1 :a 2 :b 3)" and returns 5
</pre>
<blockquote>
<div class="indented">
<p>
Since a bacro (normally) sheds its define-time environment:
</p>
<pre class="indented">
(define call-bac
(let ((<em class=red>x</em> 2))
(define-bacro (m a) `(+ ,a ,<em class=red>x</em>))))