-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilityLoL.lisp
executable file
·143 lines (121 loc) · 3.68 KB
/
utilityLoL.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
(defun mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args) (princ a s))))
(defun symb (&rest args)
(values (intern (apply #'mkstr args)))
)
(defun group (source n)
(if (zerop n) (error "zero length"))
(labels ((rec (source acc)
(let ((rest (nthcdr n source)))
(if (consp rest)
(rec rest (cons
(subseq source 0 n)
acc))
(nreverse
(cons source acc))))))
(if source (rec source nil) nil))
)
(defun longer (x y)
(labels ((compare (x y)
(and (consp x)
(or (null y)
(compare (cdr x) (cdr y))))))
(if (and (listp x) (listp y))
(compare x y)
(> (length x) (length y)))))
(defun flatten (x)
(labels ((rec (x acc)
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec
(car x)
(rec (cdr x) acc))))))
(rec x nil)))
(defun flatten_SBCL (x)
(labels ((flatten-recursively (x flattening-list)
(cond ((null x) flattening-list)
((eq (type-of x) 'SB-IMPL::COMMA) (flatten-recursively (sb-impl::comma-expr x) flattening-list))
((atom x) (cons x flattening-list))
(t (flatten-recursively (car x) (flatten-recursively (cdr x) flattening-list))))))
(flatten-recursively x nil)))
(defun g!-symbol-p (s)
(and (symbolp s)
(> (length (symbol-name s)) 2)
(string= (symbol-name s)
"G!"
:start1 0
:end1 2))
)
(defmacro defmacro/g! (name args &rest body)
(let ((syms (remove-duplicates
(remove-if-not #'g!-symbol-p
(flatten_SBCL body)))))
`(defmacro ,name ,args
(let ,(mapcar
(lambda (s)
`(,s (gensym ,(subseq
(symbol-name s)
2))))
syms)
,@body))))
(defmacro/g! nif (expr pos zero neg)
`(let ((,g!result ,expr))
(cond ((plusp ,g!result) ,pos)
((zerop ,g!result) ,zero)
(t ,neg))
))
;(nif -3 (princ "+") (princ "0")(princ "-"))
(defun fact (x)
(if (= x 0)
1
(* x (fact (- x 1))))
)
(defun choose (n r)
(/ (fact n)
(fact (- n r))
(fact r))
)
(defun tarai (x y z)
(if (<= x y)
y
(tarai
(tarai (1- x) y z)
(tarai (1- y) z x)
(tarai (1- z) x y))))
(defun primep (n)
#+sbcl (sb-int:positive-primep n))
(defun cortz (n)
(if (= n 1)
1
(if (evenp n)
(cortz (/ n 2))
(cortz (+ (* n 3) 1)))))
(defun dots(num)
(let ((dot-string ""))
(labels ((dot (n tmp)
(if (= n 0)
tmp
(dot (- n 1) (concatenate 'string tmp ".")))))
(if(= num 0)
dot-string
(dot num dot-string))))
)
(defun dots2(num)
(let ((result ""))
(do ((i 0 (+ i 1)))
((> i (- num 1) )result)
(setf result (concatenate 'string result ".")))))
(defmacro/g! ntimes (n &rest body)
`(do ((x 0 (+ x 1)))
((>= x ,n))
,@body))
(defmacro/g! for (var start stop &body body)
(let ((gstop (gensym)))
`(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
;(setf path (make-pathname :name "myfile1"))
;(setf str (open path :direction :output :if-exists :supersede))
;(close str)