-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlib.lisp
4235 lines (3605 loc) · 152 KB
/
mlib.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
;;;; === Mlib - Default library for Murmel
;;;
;;; mlib adds commonly used Lisp functions and macros to the
;;; [core Murmel language (see murmel-langref.md)](murmel-langref.md).
;;;
;;; Most of mlib's functions and macros are modeled after Common Lisp,
;;; (often with reduced functionality) plus some additional macros and functions.
;;; == Usage
;;;
;;; Copy `mlib.lisp` into the directory containing `jmurmel.jar`
;;; or into the directory specified with `--libdir`
;;; and begin your source file with
;;;
;;; (require "mlib")
;;; == mlib functions and macros
;;;
;;; mlib provides the following Common Lisp-like functions and macros:
;;;
;;; - logic, program structure
;;; - [when](#macro-when), [unless](#macro-unless)
;;; - [not](#function-not), [and](#macro-and), [or](#macro-or)
;;; - [prog1, prog2](#macro-prog1-prog2)
;;; - [case](#macro-case), [typecase](#macro-typecase)
;;; - conses and lists
;;; - [caar..cdddr](#function-caarcdddr), [nthcdr, dotted-nthcdr, nth](#function-nthcdr-dotted-nthcdr-nth), [endp](#function-endp)
;;; - [copy-list](#function-copy-list), [copy-alist](#function-copy-alist), [copy-tree](#function-copy-tree)
;;; - [list-length](#function-list-length), [last](#function-last), [butlast](#function-butlast), [nbutlast](#function-nbutlast), [ldiff](#function-ldiff), [tailp](#function-tailp)
;;; - [subst](#function-subst), [subst-if](#function-subst-if), [nsubst](#function-nsubst), [nsubst-if](#function-nsubst-if)
;;; - [nconc](#function-nconc), [revappend, nreconc](#function-revappend-nreconc), [member](#function-member), [adjoin](#function-adjoin)
;;; - [acons](#function-acons)
;;; - [mapcar](#function-mapcar), [maplist](#function-maplist), [mapc](#function-mapc), [mapl](#function-mapl), [mapcan](#function-mapcan), [mapcon](#function-mapcon)
;;; - [multiple-value-list](#macro-multiple-value-list), [nth-value](#macro-nth-value)
;;; - iteration
;;; - [do, do*](#macro-do-do), [dotimes](#macro-dotimes), [dolist](#macro-dolist)
;;; - places
;;; - [destructuring-bind](#macro-destructuring-bind)
;;; - [get-setf-expansion](#function-get-setf-expansion)
;;; - [setf](#macro-setf), [psetf](#macro-psetf), [shiftf](#macro-shiftf), [rotatef](#macro-rotatef)
;;; - [incf, decf](#macro-incf-decf)
;;; - [push](#macro-push), [pop](#macro-pop), [pushnew](#macro-pushnew)
;;; - numbers, characters
;;; - [abs](#function-abs), [min](#function-min), [max](#function-max), [zerop](#function-zerop), [evenp](#function-evenp), [oddp](#function-oddp)
;;; - [char=](#function-char), [char](#function-char-1), [bit](#function-bit)
;;; - [parse](#function-parse), [parse-integer](#function-parse-integer)
;;; - sequences
;;; - [elt](#function-elt), [copy-seq](#function-copy-seq), [length](#function-length)
;;; - [reverse](#function-reverse), [nreverse](#function-nreverse)
;;; - [remove-if](#function-remove-if), [remove](#function-remove)
;;; - [concatenate](#function-concatenate)
;;; - [map](#function-map), [map-into](#function-map-into), [reduce](#function-reduce)
;;; - hash tables
;;; - [gethash](#function-gethash), [remhash](#function-remhash), [maphash](#function-maphash)
;;; - higher order
;;; - [identity](#function-identity), [constantly](#function-constantly), [complement](#function-complement)
;;; - [every](#function-every), [some](#function-some), [notevery](#function-notevery), [notany](#function-notany)
;;; - I/O
;;; - [write-char](#function-write-char)
;;; - [terpri, prin1, princ, print](#function-terpri-prin1-princ-print), [pprint](#function-pprint)
;;; - [format](#function-format), [formatter](#macro-formatter)
;;; - [error](#function-error)
;;; - [with-output-to-string](#macro-with-output-to-string)
;;; - misc
;;; - [time](#macro-time)
;;;
;;; functions and macros inspired by [Alexandria](https://alexandria.common-lisp.dev):
;;;
;;; - conses and lists
;;; - [circular-list](#function-circular-list)
;;; - [mappend](#function-mappend), [mappend-tails](#function-mappend-tails)
;;; - iteration
;;; - [doplist](#macro-doplist)
;;; - higher order
;;; - [compose](#function-compose), [multiple-value-compose](#function-multiple-value-compose)
;;; - [conjoin](#function-conjoin), [disjoin](#function-disjoin)
;;; - [curry](#function-curry), [rcurry](#function-rcurry)
;;; - misc
;;; - [with-gensyms](#macro-with-gensyms)
;;;
;;; functions inspired by [SRFI-1](https://srfi.schemers.org/srfi-1/srfi-1.html)
;;;
;;; - conses and lists
;;; - [unzip](#function-unzip)
;;;
;;; functions and macros inspired by [serapeum](https://github.com/ruricolist/serapeum/blob/master/REFERENCE.md)
;;;
;;; - conses and lists
;;; - [plist-keys](#function-plist-keys), [plist-values](#function-plist-values)
;;; - misc
;;; - [with-accumulator](#macro-with-accumulator), [summing](#macro-summing), [collecting](#macro-collecting), [reverse-collecting](#macro-reverse-collecting)
;;;
;;; as well as the following additional functions and macros:
;;;
;;; - logic and program structure
;;; - [->](#macro), [->>](#macro-1), [and->](#macro-and-1), [and->>](#macro-and-2)
;;; - conses and lists
;;; - [unzip-tails](#function-unzip-tails)
;;; - iteration
;;; - [dovector](#macro-dovector), [dogenerator](#macro-dogenerator)
;;; - places
;;; - [*f, /f, +f, -f](#macro-f-f)
;;; - generators
;;; - [scan](#function-scan), [scan-multiple](#function-scan-multiple), [scan-concat](#function-scan-concat)
;;; - strings
;;; - [string-trim](#function-string-trim), [string-subseq](#function-string-subseq), [string-replace](#function-string-replace), [string-split](#function-string-split), [string-join](#function-string-join)
;;; == Description of functions and macros
; logic, program structure ********************************************
;;; = Macro: when
;;; (when condition forms*) -> result
;;;
;;; Since: 1.1
;;;
;;; Execute `forms` if `condition` evaluates to true
;;; and return the result of the last form if any.
;;; Otherwise if `condition` evaluates to false,
;;; the forms are not evaluated and the return value
;;; of the `when`-form is `nil`.
(defmacro when (condition . body)
(list 'if
condition
(if (cdr body)
(cons 'progn body)
(car body))))
;;; = Macro: unless
;;; (unless condition forms*) -> result
;;;
;;; Since: 1.1
;;;
;;; Execute `forms` if `condition` evaluates to false
;;; and return the result of the last form if any.
;;; Otherwise if `condition` evaluates to true,
;;; the forms are not evaluated and the return value
;;; of the `unless`-form is `nil`.
(defmacro unless (condition . body)
(list 'if
condition
()
(if (cdr body)
(cons 'progn body)
(car body))))
;;; = Function: not
;;; (not form) -> boolean
;;;
;;; Since: 1.1
;;;
;;; Logical not.
(define not null)
(defmacro not (form)
`(null ,form))
;;; = Macro: and
;;; (and forms*) -> result
;;;
;;; Since: 1.1
;;;
;;; Short-circuiting logical and.
;;; Return `t` if no forms were given,
;;; otherwise return the values resulting from the evaluation of the last form unless any of the `forms` evaluate to `nil`,
;;; `nil` otherwise.
(defmacro and forms
(if forms
(if (cdr forms)
`(if ,(car forms)
(and ,@(cdr forms)))
(car forms))
t))
;;; = Macro: or
;;; (or forms*) -> result
;;;
;;; Since: 1.1
;;;
;;; Short-circuiting logical or.
;;; Return `nil` unless any of the `forms` evaluate to non-nil,
;;; the result of the first form returning non-nil otherwise.
(defmacro or forms
(labels ((m%or (tmp forms)
(when forms
(if (cdr forms)
`(if (setq ,tmp ,(car forms))
,tmp
,(m%or tmp (cdr forms)))
(car forms)))))
;; strip off any leading nil
(let loop ()
(when forms
(when (null (car forms))
(setq forms (cdr forms))
(loop))))
(when forms
(if (cdr forms)
(let ((temp (gensym)))
`(let ((,temp ,(car forms)))
(if ,temp
,temp
,(m%or temp (cdr forms)))))
(car forms)))))
;;; = Macro: prog1, prog2
;;; (prog1 first-form more-forms*) -> result-1
;;; (prog2 first-form second-form more-forms*) -> result-2
;;;
;;; Since: 1.1
(defmacro prog1 (first-form . more-forms)
(if more-forms
(let ((result (gensym)))
`(let ((,result ,first-form))
,@more-forms
,result))
`(values ,first-form)))
(defmacro prog2 (first-form second-form . more-forms)
(if more-forms
(let ((result (gensym)))
`(progn
,first-form
(let ((,result ,second-form))
,@more-forms
,result)))
`(progn ,first-form (values ,second-form))))
;;; = Macro: case
;;; (case keyform (keys forms*)* [(t forms*)]) -> result
;;;
;;; Since: 1.1
;;;
;;; `keys` can be a single key or a list of keys, keys will not be evaluated.
;;; `keyform` will be matched against `keys` using `eql`, the `forms` of the
;;; matching clause will be eval'd and the last form determines the result.
;;; Subsequent clauses will be ignored.
;;;
;;; A clause with a key that is a single `t` is used as the default clause
;;; if no key matches.
(defmacro case (keyform . clauses)
(labels ((do-key (tmp key)
(if (symbolp key)
`(eq ,tmp ',key)
`(eql ,tmp ',key)))
(do-keylist (tmp keylist)
(if (cdr keylist)
`(or ,@(mapcar (lambda (k) (do-key tmp k)) keylist))
(do-key tmp (car keylist))))
(do-clause (tmp clause)
(let ((keydesignator (car clause))
(forms (cdr clause)))
(if keydesignator
(if (consp keydesignator)
(list* (do-keylist tmp keydesignator) forms)
(if (eq 't keydesignator)
`(t ,@forms)
`(,(do-key tmp keydesignator) ,@forms))))))
(do-clauses (key)
(let* ((result (list ()))
(append-to result)
clause)
(let loop ((clauses clauses))
(when clauses
(setq clause (do-clause key (car clauses)))
(if clause (setq append-to (cdr (rplacd append-to (list clause)))))
(loop (cdr clauses))))
(cdr result))))
(if (atom keyform)
`(cond ,@(do-clauses keyform))
(let ((tmp (gensym)))
`(let ((,tmp ,keyform))
(cond ,@(do-clauses tmp)))))))
;;; = Macro: typecase
;;; (typecase keyform (type forms*)* [(t forms*)]) -> result
;;;
;;; Since: 1.3
;;;
;;; `typecase` allows the conditional execution of a body of forms in a clause
;;; that is selected by matching the test-key on the basis of its type.
;;;
;;; The keyform is evaluated to produce the test-key.
;;;
;;; Each of the normal-clauses is then considered in turn.
;;; If the test-key is of the type given by the clauses's type,
;;; the forms in that clause are evaluated as an implicit progn,
;;; and the values it returns are returned as the value of the typecase form.
;;;
;;; If no normal-clause matches, and there is an otherwise-clause,
;;; then that otherwise-clause automatically matches;
;;; the forms in that clause are evaluated as an implicit progn,
;;; and the values it returns are returned as the value of the typecase.
;;;
;;; If there is no otherwise-clause, typecase returns nil.
(defmacro typecase (keyform . clauses)
(labels ((do-clause (tmp clause)
(let ((keydesignator (car clause)))
(if (and keydesignator (symbolp keydesignator))
`((typep ,tmp ',keydesignator) ,@(cdr clause))
(jerror 'simple-error "typecase - bad clause '%s'" clause))))
(do-clauses (key)
(let* ((result (list ()))
(append-to result))
(let loop ((clauses clauses))
(when clauses
(setq append-to (cdr (rplacd append-to (list (do-clause key (car clauses))))))
(loop (cdr clauses))))
(cdr result))))
(if (atom keyform)
`(cond ,@(do-clauses keyform))
(let ((tmp (gensym)))
`(let ((,tmp ,keyform))
(cond ,@(do-clauses tmp)))))))
; conses and lists ****************************************************
(defmacro m%def-macro-fun (name params . body)
"Expand into a defmacro as well as a defun.
Must only be used when body uses each parameter exactly once in the given order."
`(progn
(defmacro ,name ,params ,@body)
(defun ,name ,params (,name ,@params))))
;;; = Function: caar..cdddr
;;; (c..r lst) -> result
;;;
;;; Since: 1.1
;;;
;;; `c..r` repeatedly apply `car` and/ or `cdr` as the name suggests.
(m%def-macro-fun caar (lst) `(car (car ,lst)))
(m%def-macro-fun cadr (lst) `(car (cdr ,lst)))
(m%def-macro-fun cdar (lst) `(cdr (car ,lst)))
(m%def-macro-fun cddr (lst) `(cdr (cdr ,lst)))
(m%def-macro-fun caaar (lst) `(car (caar ,lst)))
(m%def-macro-fun caadr (lst) `(car (cadr ,lst)))
(m%def-macro-fun cadar (lst) `(car (cdar ,lst)))
(m%def-macro-fun caddr (lst) `(car (cddr ,lst)))
(m%def-macro-fun cdaar (lst) `(cdr (caar ,lst)))
(m%def-macro-fun cdadr (lst) `(cdr (cadr ,lst)))
(m%def-macro-fun cddar (lst) `(cdr (cdar ,lst)))
(m%def-macro-fun cdddr (lst) `(cdr (cddr ,lst)))
;;; = Function: endp
;;; (endp list) -> boolean
;;;
;;; Since: 1.4.6
;;;
;;; This is the recommended way to test for the end of a proper list. It
;;; returns true if `obj` is `nil`, false if `obj` is a `cons`,
;;; and a `type-error` for any other type of `object`.
(defun endp (obj)
(cond ((consp obj) nil)
((null obj) t)
(t (jerror 'simple-type-error "endp - not a list: '%s'" obj))))
(defun m%nonneg-integer-number (n)
(cond ((integerp n)
(if (< n 0)
#1=(jerror 'simple-type-error "must be an integer >= 0: '%s'" n)
n))
((numberp n)
(if (< n 0) #1#
(let ((ntrunc (truncate n)))
(if (/= n ntrunc) #1#
ntrunc))))
(t #1#)))
;;; = Function: nthcdr, dotted-nthcdr, nth
;;; (nthcdr n lst) -> nth-tail
;;; (dotted-nthcdr n lst) -> nth-tail
;;; (nth n lst) -> nth-element
;;;
;;; Since: 1.1
;;;
;;; `nthcdr` applies `cdr` n times and returns the result.
;;; `dotted-nthcdr` works similar to `nthcdr` except:
;;; going past the end of a dotted list returns `nil`
;;; (and not an error as `nthcdr` would).
;;; `nth` works as if `(car (nthcdr n lst))` was invoked.
(defun nthcdr (n lst)
(let loop ((n (m%nonneg-integer-number n)) (lst lst))
(if (<= n 0)
lst
(when lst (loop (1- n) (cdr lst))))))
; For [n]butlast
(defun dotted-nthcdr (n lst)
(let loop ((n (m%nonneg-integer-number n)) (lst lst))
(if (<= n 0)
lst
(when (consp lst) (loop (1- n) (cdr lst))))))
(m%def-macro-fun nth (n lst)
`(car (nthcdr ,n ,lst)))
;;; = Function: copy-list
;;;
;;; (copy-list lst) -> copy
;;;
;;; Since: 1.3
;;;
;;; Returns a copy of `lst`. If `lst` is a dotted list,
;;; the resulting list will also be a dotted list.
;;;
;;; Only the list structure of `lst` is copied;
;;; the elements of the resulting list are the same
;;; as the corresponding elements of the given list.
(defun copy-list (lst)
(let* loop ((lst lst)
(result (list ()))
(append-to result))
(if (consp lst)
(loop (cdr lst) result (cdr (rplacd append-to (list (car lst)))))
(progn
(when lst (rplacd append-to lst))
(cdr result)))))
;;; = Function: unzip
;;; (unzip lists) -> result-list
;;;
;;; Since: 1.2
;;;
;;; `unzip` takes a list of lists, and returns a list
;;; containing the initial element of each such list,
;;; e.g.:
;;;
;;; (unzip '((1 2) (11 22) (111 222))) ; ==> (1 11 111)
;;; (unzip '(nil nil nil)) ; ==> (nil nil nil)
;;; (unzip nil) ; ==> nil
;;;
;;; Similar to SRFI-1 `unzip1`, see https://srfi.schemers.org/srfi-1/srfi-1.html#unzip1.
;;;
;;; See also: [unzip-tails](#function-unzip-tails).
(defun unzip (lists)
(when lists
(cons (caar lists) (unzip (cdr lists)))))
;;; = Function: unzip-tails
;;; (unzip-tails lists) -> result-list
;;;
;;; Since: 1.2
;;;
;;; `unzip-tails` takes a list of lists, and returns a list
;;; containing the `cdr`s of each such list.
;;;
;;; See also: [unzip](#function-unzip).
(defun unzip-tails (lists)
(when lists
(cons (cdar lists) (unzip-tails (cdr lists)))))
;;; = Function: list-length
;;; (list-length list) -> length
;;;
;;; Since: 1.1
;;;
;;; Returns the length of `list` if it is a string or proper list.
;;; Returns `nil` if `list-or-string` is a circular list.
(defun list-length (lst)
;; see http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node149.html
(let loop ((n 0) ; Counter
(fast lst) ; Fast pointer: leaps by 2
(slow lst)) ; Slow pointer: leaps by 1
;; If fast pointer hits the end, return the count.
(cond
((null fast) n)
((null (cdr fast)) (1+ n))
;; If fast pointer eventually equals slow pointer,
;; then we must be stuck in a circular list.
;; (A deeper property is the converse: if we are
;; stuck in a circular list, then eventually the
;; fast pointer will equal the slow pointer.
;; That fact justifies this implementation.)
((and (eq fast slow) (> n 0)) nil)
(t (loop (1+ (1+ n)) (cddr fast) (cdr slow))))))
;;; = Function: last
;;; (last lst [n]) -> tail
;;;
;;; Since: 1.2
;;;
;;; `last` returns the last `n` conses (not the last `n` elements)
;;; of a proper or dotted list or `nil` for the empty list.
;;;
;;; If `n` is zero, the atom that terminates list is returned.
;;; If `n` is greater than or equal to the number of cons cells in list,
;;; the result is `lst`.
(macrolet ((m%last0-macro ()
`(let loop ((rest lst))
(if (consp rest)
(loop (cdr rest))
rest)))
(m%last1-macro ()
`(let loop ((rest lst))
(if (consp (cdr rest))
(loop (cdr rest))
rest)))
;; m%lastn-macro won't work for n <= 0.
;; This causes no ill effect because the code below avoids this,
;; and then m%lastn-macro is undefined so that user code doesn't see it.
(m%lastn-macro ()
(let ((scan (gensym "scan"))
(pop (gensym "pop")))
`(let ((returned-lst lst)
(checked-lst lst)
(n n))
(let ,scan ()
(setq checked-lst (cdr checked-lst))
(if (atom checked-lst)
returned-lst
(if (= (setq n (1- n)) 0)
(let ,pop ()
(setq returned-lst (cdr returned-lst)
checked-lst (cdr checked-lst))
(if (atom checked-lst)
returned-lst
(,pop)))
(,scan))))))))
(defun m%last0 (lst)
(m%last0-macro))
(defun m%last1 (lst)
(m%last1-macro))
(defun m%lastn (lst n)
(setq n (m%nonneg-integer-number n))
(cond
((= n 1) (m%last1-macro))
((> n 1) (m%lastn-macro))
(t (m%last0-macro))))
(defun last (lst . n)
(if n
(m%lastn lst (car n))
(m%last1-macro)))
(defmacro last (lst . n)
(if n
(if (integerp (setq n (car n)))
(cond
((= n 1) `(m%last1 ,lst))
((= 0 1) `(m%last0 ,lst))
(t `(m%lastn ,lst ,n)))
`(m%lastn ,lst ,n))
`(m%last1 ,lst)))
) ; macrolet
;;; = Function: nconc
;;; (nconc lists*) -> concatenated-list
;;;
;;; Since: 1.2
;;;
;;; `nconc` concatenates lists, each list but the last is modified.
;;; If no lists are supplied, `nconc` returns `nil`.
;;; Each argument but the last must be a proper or dotted list.
(defun nconc lists
(let* outer ((outer-lists lists)
(result (car outer-lists)))
(if outer-lists
(cond
((consp result)
(let ((splice result))
(let* inner ((inner-lists (cdr outer-lists))
(ele (car inner-lists)))
(if inner-lists
(cond
((consp ele) (rplacd (last splice) ele) (setq splice ele) (inner (cdr inner-lists) (cadr inner-lists)))
((null ele) (rplacd (last splice) ()) (inner (cdr inner-lists) (cadr inner-lists)))
((atom ele) (if (cdr inner-lists)
(jerror 'simple-type-error "nconc - not a list: '%s'" ele)
(rplacd (last splice) ele)))))))
result)
((null result)
(outer (cdr outer-lists) (cadr outer-lists)))
((atom result)
(if (cdr outer-lists)
(jerror 'simple-type-error "nconc - not a list: '%s'" result)
result))))))
;;; = Function: revappend, nreconc
;;; (revappend list tail) -> result-list
;;; (nreconc list tail) -> result-list
;;;
;;; Since: 1.3
;;;
;;; `revappend` constructs a copy of `list`, but with the elements in reverse order.
;;; It then appends (as if by `nconc`) the `tail` to that reversed list and returns the result.
;;;
;;; `nreconc` reverses the order of elements in list (as if by `nreverse`).
;;; It then appends (as if by `nconc`) the tail to that reversed list and returns the result.
;;;
;;; The resulting list shares list structure with tail.
;;;
;;; (revappend x y) ::= (append (reverse x) y)
;;; (nreconc x y) ::= (nconc (nreverse x) y)
(defun revappend (x y)
(if x
(revappend (cdr x) (cons (car x) y))
y))
(defun nreconc (x y)
(let loop ((1st (cdr x))
(2nd x)
(3rd y))
(if (atom 2nd)
3rd
(progn
(rplacd 2nd 3rd)
(loop (if (null 1st) 1st (cdr 1st)) 1st 2nd)))))
;;; = Function: member
;;; (member item list [test]) -> tail
;;;
;;; Since: 1.1
;;;
;;; `member` searches list for `item` or for a top-level element that
;;; satisfies the `test`.
;;;
;;; `test` if given must be a function that takes two arguments.
;;; If `test` was omitted or `nil` then `eql` will be used.
;;;
;;; Example usage:
;;;
;;; (member 2 '(1 2 3))
;;; ; => (2 3)
;;; (member 'e '(a b c d))
;;; ; => NIL
;;; (member '(1 . 1) '((a . a) (b . b) (c . c) (1 . 1) (2 . 2) (3 . 3))
;;; equal)
;;; ; => ((1 . 1) (2 . 2) (3 . 3))
;;; (member 'c '(a b c 1 2 3) eq)
;;; ; => (c 1 2 3)
;;; (member 'b '(a b c 1 2 3) (lambda (a b) (eq a b)))
;;; ; => (b c 1 2 3)
(defun member (item lst . test)
(let* ((pred (if test (car test) eql)))
(let loop ((lst lst))
(when lst
(if (pred item (car lst))
lst
(loop (cdr lst)))))))
;;; = Function: adjoin
;;; (adjoin item list [test]) -> result-list
;;;
;;; Since: 1.4.5
;;;
;;; Tests whether `item` is the same as an existing element of `lst`.
;;; If the `item` is not an existing element, `adjoin` adds it to `lst` (as if by `cons`)
;;; and returns the resulting list; otherwise, nothing is added and the original list is returned.
(defun adjoin (item lst . test)
(if (apply member (list* item lst test))
lst
(cons item lst)))
;;; = Function: acons
;;; (acons key datum alist) -> new-alist
;;;
;;; Since: 1.1
;;;
;;; Prepends `alist` with a new `(key . datum)` tuple
;;; and returns the modified list.
(defun acons (key datum alist)
(cons (cons key datum) alist))
(macrolet ((m%notany-null (lst)
(let ((loop (gensym "loop"))
(l (gensym "lst")))
`(let ,loop ((,l ,lst))
(if ,l
(when (car ,l) (,loop (cdr ,l)))
t))))
;; Helper macros to generate defuns for the various mapXX functions
(m%mapx (name acc accn)
`(defun ,name (func lst . more-lists)
(if more-lists
(let loop ((args (cons lst more-lists)))
(when (m%notany-null args)
(apply func ,(if accn (list accn 'args) 'args))
(loop (unzip-tails args))))
(let loop ((lst lst))
(when lst
(func ,(if acc (list acc 'lst) 'lst))
(loop (cdr lst)))))
lst))
(m%mapx-cons (name acc accn)
`(defun ,name (func lst . more-lists)
(let* ((result (list ())) (append-to result))
(if more-lists
(let loop ((args (cons lst more-lists)))
(when (m%notany-null args)
(setq append-to (cdr (rplacd append-to (list (apply func ,(if accn (list accn 'args) 'args))))))
(loop (unzip-tails args))))
(let loop ((lst lst))
(when lst
(setq append-to (cdr (rplacd append-to (list (func ,(if acc (list acc 'lst) 'lst))))))
(loop (cdr lst)))))
(cdr result))))
(m%mapx-nconc (name acc accn)
`(defun ,name (func lst . more-lists)
(labels ((m%last (lst)
;; returns the last cdr, similar to `last` but errors if the last cdr is a dotted pair
;;(if (consp (cdr lst)) ;; with this instead of the next line Murmel is somewhat sloppy re: mapcon/ mapcon, see https://gitlab.common-lisp.net/cmucl/cmucl/-/issues/196
(if (cdr lst)
(m%last (cdr lst))
lst)))
(let* ((result (list ())) (append-to result))
(if more-lists
(let loop ((args (cons lst more-lists)))
(when (m%notany-null args)
(setq append-to (m%last append-to))
(rplacd append-to (apply func ,(if accn (list accn 'args) 'args)))
(loop (unzip-tails args))))
(let loop ((lst lst))
(when lst
(setq append-to (m%last append-to))
(rplacd append-to (func ,(if acc (list acc 'lst) 'lst)))
(loop (cdr lst)))))
(cdr result)))))
(m%mapx-append (name acc accn)
`(defun ,name (func lst . more-lists)
(let* ((result (list ())) (append-to result))
(if more-lists
(let loop ((args (cons lst more-lists)))
(when (m%notany-null args)
(let loop ((r (apply func ,(if accn (list accn 'args) 'args))))
#1=(if (consp r)
(progn
(setq append-to (cdr (rplacd append-to (list (car r)))))
(loop (cdr r)))
(if r (jerror 'simple-type-error "%s - not a list: '%s'" ',name r))))
(loop (unzip-tails args))))
(let loop ((lst lst))
(when lst
(let loop ((r (func ,(if acc (list acc 'lst) 'lst))))
#1#)
(loop (cdr lst)))))
(cdr result)))))
;;; = Function: mapcar
;;; (mapcar function list+) -> list
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent items of the given lists.
;;; All `function` application results will be combined into a list
;;; which is the return value of `mapcar`.
(m%mapx-cons mapcar car unzip)
;;; = Function: maplist
;;; (maplist function list+) -> list
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent tails of the given lists.
;;;
;;; All `function` application results will be combined into a list
;;; which is the return value of `maplist`.
(m%mapx-cons maplist nil nil)
;;; = Function: mapc
;;; (mapc function list+) -> first-arg
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent cars items of the given lists.
(m%mapx mapc car unzip)
;;; = Function: mapl
;;; (mapl function list+) -> first-arg
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent tails of the given lists.
(m%mapx mapl nil nil)
;;; = Function: mapcan
;;; (mapcan function list+) -> concatenated-results
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent items of the given lists.
;;;
;;; All function application results will be concatenated (as if by nconc) to a list
;;; which is the return value of `mapcan`.
(m%mapx-nconc mapcan car unzip)
;;; = Function: mapcon
;;; (mapcon function list+) -> concatenated-results
;;;
;;; Since: 1.1
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent tails of the given lists.
;;;
;;; All function application results will be concatenated (as if by nconc) to a list
;;; which is the return value of `mapcon`.
(m%mapx-nconc mapcon nil nil)
; Alexandria: conses and lists ****************************************
;;; = Function: mappend
;;; (mappend function list+) -> appended-results
;;;
;;; Since: 1.4.7
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent items of the given lists.
;;;
;;; All function application results will be concatenated to a list
;;; which is the return value of `mappend`.
;;; `function` must return a list which will not be mutated by `mappend`.
;;;
;;; `mappend` works similar to Alexandria's `mappend` and
;;; can be thought of as a non-destructive version of `mapcan`,
;;; i.e. `mappend` combines the results of applying `function`
;;; by the use of `append` rather than `nconc`.
(m%mapx-append mappend car unzip)
;;; = Function: mappend-tails
;;; (mappend-tails function list+) -> appended-results
;;;
;;; Since: 1.4.7
;;;
;;; `function` must accept as many arguments as lists are given,
;;; and will applied to subsequent tails of the given lists.
;;;
;;; All function application results will be concatenated to a list
;;; which is the return value of `mappend-tails`.
;;; `function` must return a list which will not be mutated by `mappend-tails`.
;;;
;;; `mappend-tails` can be thought of as a non-destructive version of `mapcon`,
;;; i.e. `mappend-tails` combines the results of applying `function`
;;; by the use of `append` rather than `nconc`.
(m%mapx-append mappend-tails nil nil)
) ; (macrolet...
;;; = Macro: multiple-value-list
;;;
;;; Since: 1.4
(defmacro multiple-value-list (value-form)
`(multiple-value-call list ,value-form))
;;; = Macro: nth-value
;;;
;;; Since: 1.4
(defmacro nth-value (n value-form)
`(nth ,n (multiple-value-list ,value-form)))
; iteration ***********************************************************
;;; = Macro: do, do*
;;; (do ({var | (var [init-form [step-form]])}*)
;;; (end-test-form result-form*)
;;; statement*) -> result
;;;
;;; (do* ({var | (var [init-form [step-form]])}*)
;;; (end-test-form result-form*)
;;; statement*) -> result
;;;
;;; Since: 1.1
;;;
;;; `do` and `do*` iterate over a group of statements while `end-test-form` returns `nil`.
(defmacro do (var-defs test-and-result . forms)
(labels ((init-form (l)
(if (symbolp l)
(list l nil)
(list (car l) (cadr l))))
(step-form (l)
(if (symbolp l)
l
(if (caddr l)
(caddr l)
(car l)))))
(let ((loop (gensym)))
`(let ,loop (,@(mapcar init-form var-defs))
(if ,(car test-and-result)
(progn ,@(cdr test-and-result))
(progn
,@forms
(,loop ,@(mapcar step-form var-defs))))))))
(defmacro do* (var-defs test-and-result . forms)
(labels ((init-form (l)
(if (symbolp l)
(list l nil)
(list (car l) (cadr l))))
(step-form (l)
(if (symbolp l)
nil
(when (caddr l)
`((setq ,(car l) ,(caddr l)))))))
(let ((loop (gensym)))
`(let* (,@(mapcar init-form var-defs))
(let ,loop ()
(if ,(car test-and-result)
(progn ,@(cdr test-and-result))
(progn
,@forms
,@(mapcan step-form var-defs)
(,loop))))))))
;;; = Macro: dotimes
;;; (dotimes (var count-form result-form*) statement*) -> result
;;;
;;; Since: 1.1
;;;
;;; Similar to CL `dotimes`.
;;; Murmel however supports multiple result-forms which will be eval'd in an
;;; implicit `progn`, similar to `do` and `do*`;
;;;
;;; Sample usage:
;;;