-
Notifications
You must be signed in to change notification settings - Fork 1
/
ll-util.lisp
1640 lines (1387 loc) · 31.5 KB
/
ll-util.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
(load "ll-load.lisp")
(ll-load "old-ttt/src/load")
(ll-load-subdir "el_parse" "process-sentence1.lisp") ; for hide-ttt-ops and unhide-ttt-ops
(ll-load "ll-cache.lisp")
(defparameter *MY-LOAD-PATHNAME* *load-pathname*)
(defparameter *DBG-TAGS* (list
; put debug tags you want here
;'matched-wffs
;'match-wff
;'process-story
;'match-inst
;'unify-wffs
;'cur1
;'unify
;'coref
;'match
;'time
;'shuffle-match
))
(defparameter *DBG-ALL* nil)
(defparameter *CANONICAL-HT*
(make-hash-table :test #'equal)
)
(defparameter *EMPTY-HT*
(make-hash-table :test #'equal)
)
(defparameter *LDEFUN-CALLS* (make-hash-table :test #'equal))
(defparameter *NEWLINE-STR* (format nil "~%"))
; Automatically scopes all setfs introduced inside
; a function to be local, without requiring the
; programmer to manually add let-bindings.
(defmacro ldefun (fname lambda-list &rest body)
(block ldefun-outer
(setf setf-vars
(dedupe (loop for element in (get-elements-pred body
(lambda (x) (and
(listp x)
(equal (car x) 'setf)
(not (starry-global? (second x)))
)))
if (symbolp (second element))
collect (second element)
))
)
(setf loop-vars
(dedupe (loop for loop-elem in (get-elements-pred body
(lambda (x) (and
(listp x)
(equal (car x) 'loop))
))
;if (symbolp (second el))
;collect (second el)
append (loop for in-loop-elem in loop-elem
for i from 0
if (equal in-loop-elem 'for)
collect (nth (+ i 1) loop-elem)
)
))
)
(setf bound-vars (list))
(loop for lt in (get-elements-pred body (lambda (x) (and (listp x) (equal (car x) 'let))))
append (loop for binding in (second lt)
if (and (listp lt) (symbolp (car lt)))
collect (car lt)))
(setf unscoped-vars (union setf-vars loop-vars :test #'equal))
(setf unscoped-vars (set-difference unscoped-vars lambda-list :test #'equal))
(setf unscoped-vars (set-difference unscoped-vars bound-vars :test #'equal))
(setf unscoped-vars (loop for uv in unscoped-vars if (not (boundp uv)) collect uv))
; TODO: maybe do some "static analysis" to warn
; about variables that may be evaluated before
; they're defined in this scope?
(setf body (append (list 'let unscoped-vars) body))
;(setf log-call
;`(let ((ldfc (gethash ',fname *LDEFUN-CALLS*)))
;(if (null ldfc)
;(setf (gethash ',fname *LDEFUN-CALLS*) 1)
;(setf (gethash ',fname *LDEFUN-CALLS*) (+ 1 ldfc)))))
;(setf body (list 'PROGN log-call body))
`(defun ,fname ,lambda-list ,body)
)
)
(defun neg-pred (pred)
(lambda (x) (not (funcall pred x)))
)
(defun list-of-pred (lst pred)
(cond
((not (listp lst)) nil)
(t (loop for e in lst
always (and (listp e) (equal 2 (length e)) (funcall pred (car e)))))
)
)
(defun max-idx (lst)
(cond
((and (equal 2 (length lst)) (integerp (second lst)))
(second lst))
(t (apply #'max (mapcar #'second lst)))
)
)
(defun k-most-freq (lst k)
(let (
(elem-to-freq (make-hash-table :test #'equal))
(freq-to-elems (make-hash-table :test #'equal))
return-elems
)
(block outer
; Build a frequency table for the elements.
(loop for elem in lst
do (setf (gethash elem elem-to-freq)
(if (null (gethash elem elem-to-freq))
; then
1
; else
(+ 1 (gethash elem elem-to-freq))))
)
; Invert the frequency table.
(loop for elem being the hash-keys of elem-to-freq
do (setf (gethash (gethash elem elem-to-freq) freq-to-elems)
(append
(gethash (gethash elem elem-to-freq) freq-to-elems)
(list elem)
)
)
)
; Loop through all frequencies, starting at
; the highest, and accumulate items until we
; have K or we run out.
; Since there are at most (length lst) unique
; frequencies, this still runs in linear time.
(loop for i from 1 to (length lst)
do (loop for elem in
(gethash (- (+ 1 (length lst)) i) freq-to-elems)
do (if (>= (length return-elems) k)
; then
(return-from outer return-elems)
; else
(setf return-elems
(append return-elems (list elem)))
)
)
)
(return-from outer return-elems)
)
)
)
(defun most-freq (lst)
(car (k-most-freq lst 1))
)
(defun get-elements-pred-helper (lst pred)
(let (cur-elem tmp-res final-results)
(block outer
; (format t "~s is index pair? ~s~%" lst (index-pair? lst))
(if (not (index-pair? lst))
(return-from outer nil))
(setf cur-elem (clean-idcs (car lst)))
;(format t "cur-elem is: ~s~%" cur-elem)
;(format t "pred is ~s~%" pred)
;(format t "good? ~s~%" (funcall pred cur-elem))
(setf final-results nil)
(if (funcall pred cur-elem)
; (return-from outer (list (list (clean-idcs (car lst)) (second lst)))))
(setf final-results (list (list (clean-idcs (car lst)) (second lst)))))
(if (not (listp cur-elem))
(return-from outer final-results))
;(format t "list is: ~s~%" cur-elem)
(setf final-results (append final-results (loop for e in (car lst)
do (setf cur-elem (clean-idcs (car e)))
do (setf tmp-res (get-elements-pred-helper e pred))
if (and (index-pair? tmp-res) (not (null (car tmp-res))))
do (setf tmp-res (list tmp-res))
if (not (null (car tmp-res)))
append tmp-res
)))
(return-from outer final-results)
)
)
)
(defun idx-ancestors (lst idx)
(block outer
(setf ancestors (list))
(setf idxified (el-idcs lst))
(setf cursor idxified)
(loop while (not (equal (second cursor) idx))
do (block loop-outer
(setf ancestors (append (list (list (get-element-idx lst (second cursor)) (second cursor))) ancestors))
(setf best '(nil -1))
(loop for e2 in (car cursor)
do (if (and (<= (second e2) idx) (> (second e2) (second best)))
; then
(setf best e2)
)
)
(setf cursor best)
)
)
(return-from outer ancestors)
)
)
(defun get-elements-pred-pairs (lst pred)
(get-elements-pred-helper (el-idcs lst) pred)
)
(defun get-elements-pred-idx (lst pred)
(mapcar #'second (get-elements-pred-helper (el-idcs lst) pred))
)
(defun get-elements-pred (lst pred)
(if (null lst)
; then
nil
; else
(mapcar #'car (get-elements-pred-helper (el-idcs lst) pred))
)
)
(defun index-pair? (x)
(and
(listp x)
(equal 2 (length x))
(integerp (second x))
)
)
(defun clean-idcs (lst)
(cond
((not (listp lst)) lst)
((index-pair? lst)
(clean-idcs (car lst)))
(t (loop for e in lst
collect (clean-idcs e)
))
)
)
(defun get-element-idx-helper (lst target)
(block outer
;(format t "lst is ~s~%" lst)
;(format t "target is ~s~%" target)
(if (equal (second lst) target)
(return-from outer lst))
(if (and (listp lst) (listp (car lst)))
(loop for e in (car lst)
;do (format t "e is ~s~%" e)
do (setf tmp5 (get-element-idx-helper e target))
if (not (null tmp5))
do (return-from outer tmp5)
))
(return-from outer nil)
)
)
(defun get-element-idx (lst target)
(progn
;(format t "el-idcs is ~s~%" (el-idcs lst))
;(format t "target is ~s~%" target)
(clean-idcs (get-element-idx-helper (el-idcs lst) target))
)
)
(defun replace-element-idx-helper (lst target new-val)
(block outer
(if (not (index-pair? lst))
(return-from outer nil))
; NOTE: the dummy -1 in our return value here doesn't actually
; matter, since we're stripping the indices before we return.
; In fact, it may even be incorrect, if new-val is a composite
; list. But again, it doesn't matter either way.
(if (equal target (second lst))
(return-from outer (list new-val -1)))
(if (not (listp (clean-idcs (car lst))))
(return-from outer lst))
(return-from outer (list (loop for e in (car lst)
collect (replace-element-idx-helper e target new-val)
) (second lst)))
)
)
(defun replace-element-idx (lst target new-val)
(clean-idcs (replace-element-idx-helper (el-idcs lst) target new-val))
)
(defun el-idcs-helper (lst counter)
(let ((my-counter (eval counter)))
(cond
((not (listp lst))
(list lst my-counter))
(t (list (loop for e in lst
do (set counter (+ 1 (eval counter)))
collect (el-idcs-helper e counter)
) my-counter))
)
))
(defun el-idcs (lst)
(progn
(setf tmp-sym (gensym))
(set tmp-sym 0)
(el-idcs-helper lst tmp-sym)
)
)
(defun copy-item (item)
(if (listp item)
; then
(copy-list item)
; else
item
)
)
(defun replace-vals (old new lst)
(block outer
(if (equal lst old)
(return-from outer new))
(if (not (listp lst))
(return-from outer lst))
(loop for e in lst
collect (replace-vals old new e)
)
)
)
; DFS for an element matching a predicate in
; a(n assumed-to-be-acyclic) list structure.
(defun has-element-pred (lst pred)
(cond
;((not (listp lst))
; (funcall pred lst))
((funcall pred lst) t)
((not (listp lst)) nil)
(t (loop for e in lst thereis (has-element-pred e pred)))
)
)
(defun dupes (lst)
(dedupe (let ((dupe-map (make-hash-table :test #'equal)))
(loop for e in lst
if (not (null (gethash e dupe-map)))
collect e
else
do (setf (gethash e dupe-map) t)
)
))
)
(defun dedupe (lst)
(remove-duplicates lst :test #'equal)
)
(defun dedupe-except (lst except)
(remove-duplicates lst :test
(lambda (x y)
(and
(equal x y)
(not (contains except x))
)
)
)
)
; A convenience function for a single element in a flat list.
(defun contains (lst elem)
(not (null (member elem lst :test #'equal)))
)
(defun has-element (lst elem)
(has-element-pred lst (lambda (x) (equal x elem)))
)
(defun has-all-elements-pred (lst pred)
(not (has-element-pred lst (neg-pred pred)))
)
(defun has-no-elements-pred (lst pred)
(not (has-element-pred lst pred))
)
(defun has-lst-prefix? (l pre)
(and
(listp l)
(listp pre)
(>= (length l) (length pre))
(equal
(subseq l 0 (length pre))
pre
)
)
)
(defun has-prefix? (s pre)
(and
(stringp s)
(>= (length s) (length pre))
(equal pre (subseq s 0 (length pre)))
)
)
(defun replace-first-substr (s sub rep)
(let ((spl (split-str s sub)))
(if (not (equal 1 (length spl)))
; then
(apply #'concat-strs
(append
(list (car spl) rep)
(cdr spl)
)
)
; else
s
)
)
)
(defun has-subseq-pred (s seq eqpred)
(block outer
(if (or (not (listp s)) (not (listp seq)))
(return-from outer nil)
)
(if (> (length seq) (length s))
(return-from outer nil)
)
(loop for i from 0 to (- (length s) (length seq))
if (funcall eqpred (subseq s i (+ i (length seq))) seq)
do (return-from outer t)
)
(return-from outer nil)
)
)
(defun has-subseq (s seq)
(has-subseq-pred s seq #'equal)
)
(defun has-suffix? (s suf)
(and
(stringp s)
(>= (length s) (length suf))
(equal suf (subseq s
(- (length s) (length suf))
(length s)))
)
)
(defun varp (v)
(cond
((not (symbolp v)) nil)
((not (> (length (string v)) 1)) nil)
((not (equal "?" (subseq (string v) 0 1))) nil)
((has-suffix? (string v) "<-") nil) ; subordinate skolems
(t t)
)
)
(defun remove-prefix (s pre)
(if (has-prefix? s pre)
;then
(subseq s (length pre) (length s))
; else
s
)
)
(defun remove-suffix (s suf)
(if (has-suffix? s suf)
;then
(subseq s 0 (- (length s) (length suf)))
; else
s
)
)
(defun is-letter? (c)
(or
(and
(>= (char-code c) (char-code #\a))
(<= (char-code c) (char-code #\z))
)
(and
(>= (char-code c) (char-code #\A))
(<= (char-code c) (char-code #\Z))
)
)
)
(defun is-digit? (c)
(and
(>= (char-code c) (char-code #\0))
(<= (char-code c) (char-code #\9))
)
)
(defun is-num-str? (s)
(and
(stringp s)
(loop for c across s always (is-digit? c))
)
)
(defun alpha-str? (s)
(and
(stringp s)
(loop for c across s always (alpha-char-p c))
)
)
(defun alphanum-str? (s)
(and
(stringp s)
(loop for c across s always (alphanumericp c))
(loop for c across s thereis (alpha-char-p c))
)
)
(defun num-str? (s)
(loop for c across s always (and
(alphanumericp c)
(not (alpha-char-p c))
))
)
(defun next-str (s)
(block outer
(if (not (equal s (string-upcase s)))
(return-from outer (next-str (string-upcase s)))
)
(if (equal "Z" (subseq s (- (length s) 1) (length s)))
(return-from outer (concat-strs s "A"))
)
(return-from outer (concat-strs
(subseq s 0 (- (length s) 1))
(string (code-char (+ 1 (char-code (char s (- (length s) 1))))))
))
)
)
(defun rechashtable (data)
(sxhash (append (list 'rechashtable-ht-sentinel (sort (loop for k being the hash-keys of data collect (rechash (list k (gethash k data)))) #'<))))
)
(defun rechash (data)
(cond
((hash-table-p data)
(rechashtable data)
)
((not (listp data))
(sxhash data))
;(t (sxhash (loop for e in data
; if (listp e) collect (rechash e)
; else collect e)))
(t (sxhash (mapcar (lambda (x) (if (listp x) (rechash x) x)) data)))
)
)
(defun unordered-rechash (data)
(cond
((hash-table-p data)
(rechashtable data)
)
((not (listp data))
(sxhash data))
(t (sxhash (sort (loop for e in data
if (listp e) collect (unordered-rechash e)
else collect (sxhash e)) #'<)))
)
)
(defun ht-contains (ht key)
(nth-value 1 (gethash key ht))
)
(defun ht-count (ht)
(if (or (null ht) (not (hashtablep ht)))
; then
0
; else
(hash-table-count ht)
)
)
(defun ht-copy (ht)
(block outer
(if (not (hashtablep ht))
nil
)
(setf new-ht (make-hash-table :test #'equal))
(loop for key being the hash-keys of ht
do (setf (gethash key new-ht) (gethash key ht))
)
(return-from outer new-ht)
)
)
(defun ht-eq-oneway (ht1 ht2)
(loop for key being the hash-keys of ht1
always (equal (gethash key ht1) (gethash key ht2))
)
)
(defun ht-pairs (ht)
(loop for k being the hash-keys of ht
collect (list k (gethash k ht)))
)
(defun rec-equal (l1 l2)
(block outer
(if (and (not (listp l1)) (not (listp l2)))
; then
(return-from outer (equal l1 l2))
)
(if (or (listp l1) (listp l2))
; then
(return-from outer nil)
)
(if (not (equal (length l1) (length l2)))
; then
(return-from outer nil)
)
(return-from outer
(loop for e1 in l1
for e2 in l2
always (rec-equal e1 e2)))
)
)
(defun dbg (tag fmt-str &rest args)
(if (or *DBG-ALL* (member tag *DBG-TAGS* :test #'equal))
(apply #'format (append (list t fmt-str) args))
)
)
(defun dbg-tag (tag)
(if (not (member tag *DBG-TAGS* :test #'equal))
(setf *DBG-TAGS* (append *DBG-TAGS* (list tag)))
)
)
(defun dbg-untag (tag)
(setf *DBG-TAGS* (remove tag *DBG-TAGS*))
)
(defun ht-eq (ht1 ht2)
(cond
((and (hashtablep ht1) (not (hashtablep ht2)))
nil)
((and (hashtablep ht2) (not (hashtablep ht1)))
nil)
((and (not (hashtablep ht1)) (not (hashtablep ht2)))
(equal ht1 ht2)
)
((and
(ht-eq-oneway ht1 ht2)
(ht-eq-oneway ht2 ht1)
) t)
(t nil)
)
)
(defun ht-empty? (ht)
(or (null ht) (equal 0 (hash-table-count ht)))
)
(defun xor (p q)
(or
(and p (not q))
(and q (not p))
)
)
(defun mk-hashtable (pairs &optional allow-multi)
(cond
((equal pairs t) t)
((null pairs) nil)
(t (progn
(setf want-bind (make-hash-table :test #'equal))
(loop for pair in pairs
if allow-multi
do (setf (gethash (car pair) want-bind)
(append (gethash (car pair) want-bind)
(list (second pair))))
else
do (setf (gethash (car pair) want-bind)
(second pair))
)
want-bind
))
)
)
(defun same-list-unordered (l1 l2)
(and
(listp l1)
(listp l2)
(equal (length l1) (length l2))
(loop for e in l1
always (member e l2 :test #'equal)
)
)
)
(defun verbp (x)
(and
(symbolp x)
(equal
".V"
(subseq
(string x)
(- (length (string x)) 2)
(length (string x))
)
)
)
)
(defun hashtablep (o)
(equal
(type-of o)
(type-of *CANONICAL-HT*)
)
)
(defun print-ht (ht)
(format t "~s" (ht-to-str ht))
)
(defun wrap-nonlists (x)
(if (not (listp x))
; then
(list x)
; else
x
)
)
(defun explain-nil (str &rest args)
(progn
(apply #'format (append (list t str) args))
nil
)
)
; replace-all taken from https://lispcookbook.github.io/cl-cookbook/strings.html
(defun replace-all (str part replacement &key (test #'equal))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part str
:start2 old-pos
:test test)
do (write-string str out
:start old-pos
:end (or pos (length str)))
when pos do (write-string replacement out)
while pos)))
(defun concat-two-strs (str1 str2)
; (format nil "~d~d" str1 str2)
(concatenate 'string str1 str2)
)
(defun concat-strs (&rest strs)
(cond
((null strs) nil)
((equal 1 (length strs)) (car strs))
(t (concat-two-strs
(car strs)
(apply #'concat-strs (cdr strs))))
)
)
(defparameter *B64-STR* "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(defun b10tob64 (num)
(let (b8str b64lst val) (block outer
(setf b8str (write-to-string num :base 8))
(setf b64lst (list))
(loop for i from 0 to (- (length b8str) 2)
if (equal 0 (mod i 2)) do (block inner
(setf val (- (* (+ 1 (parse-integer (subseq b8str i (+ i 1))))
(+ 1 (parse-integer (subseq b8str (+ i 1) (+ i 2))))) 1))
(setf b64lst (append b64lst (list (subseq *B64-STR* val (+ val 1)))))
)
)
(return-from outer (apply #'concat-strs b64lst))
))
)
(defun join-str-list (sep strs)
(cond
((null strs) "")
((equal (length strs) 1)
(car strs))
(t (concat-strs
(car strs)
sep
(join-str-list sep (cdr strs))))
)
)
(defun subseq-safe (seq beg end)
(let (
(real-beg (if (> beg (length seq)) (length seq) beg))
(real-end (if (> end (length seq)) (length seq) end))
)
(subseq seq (max real-beg 0) real-end)
)
)
(defun split-str (str sep)
(cond
((null (search sep str))
(list str))
(t (append
(list (subseq str 0 (search sep str)))
(split-str (subseq str (+ (length sep) (search sep str)) (length str)) sep)
))
)
)
(defun split-lst (lst sep)
(remove nil
(mapcar #'unwrap-singletons
(let ((pos (position sep lst :test #'equal)))
(cond
((null pos)
(list lst))
(t (append
(list (subseq lst 0 pos))
(split-lst (subseq lst (+ 1 pos) (length lst)) sep)))
)
)
)))
(defun repeat-str (str n)
(format nil "~v@{~A~:*~}" n str)
)
(defparameter *NEWLINE* (string #\Newline))
(defparameter *TAB* (string #\Tab))
(defun tab-all-lines (n str)
(concat-two-strs *TAB*
(replace-all str
*NEWLINE*
(concat-two-strs
*NEWLINE*
(repeat-str *TAB* n))))
)
(defun ht-to-str (ht)
(join-str-list *NEWLINE*
(cond
((not (hashtablep ht)) (list (format nil "value ~s" ht)))
(t (loop for key being the hash-keys of ht
; do (format t "GOT ~s~%" (gethash key ht))
collect (format nil "~d: ~d" key (gethash key ht))
))
)
)
)
(defun print-ht (ht)
(cond
((not (hashtablep ht)) (format t " value ~s~%" ht))
(t (loop for key being the hash-keys of ht
do (format t " ~s: ~s~%" key (gethash key ht))
))
)
)
;(defmacro check (pr)
; `(when (not ,pr)
; (error "Check failed: ~s~%" ',pr)))
(defun check (pred phi)
(if (not (funcall pred phi))
; then
(progn
(error "Check failed: (~s ~s)~%" pred phi)
nil
)
; else
t
)
)
(defun starry-global? (x)
(and
(symbolp x)
(let ((str-x (format nil "~s" x)))
(and
(has-prefix? str-x "*")
(has-suffix? str-x "*")
)
)
)
)
(defun norm-singletons (lst)
(if (and
(listp lst)
(equal 1 (length lst))
(listp (car lst))
)
; then
(norm-singletons (car lst))
; else
lst
)
)
(defun listify-nonlists (e)
(if (listp e)
; then
e
; else
(list e))
)