きざむぞ!
- 11.1 ストリーム
- 11.2 データの出力
のCL入門復習を完了。データの出力にて:prettyの効果がわからない。nilでもtでも出力が変わらんような。
じわじわ。
(defun collect-rec (sexp &optional result)
(cond
((null sexp) result)
((symbolp sexp) (cons sexp result))
(t (append (collect-rec (car sexp) result)
(collect-rec (cdr sexp) result)))))
(defun collect-rec (sexp &optional result)
(cond
((null sexp) result)
((symbolp sexp)
(if (member sexp result)
result
(cons sexp result)))
(t (append (collect-rec (car sexp) result)
(collect-rec (cdr sexp) result)))))
(defun do-symbols-in-sexp (fun sexp)
(cond
((null sexp) nil)
((symbolp sexp)
(funcall fun sexp))
(t (progn (do-symbols-in-sexp fun (car sexp))
(do-symbols-in-sexp fun (cdr sexp))))))
(setq result nil)
(do-symbols-in-sexp #'(lambda (x)
(if (not (member x result)) (push x result))) '((x j) (a d) ((x y b) . (c a))))
result ; => (C B Y X D A)
(do-symbols-in-sexp #'(lambda (x)
(if (and (not (member x '(not and or =>)))
(not (member x result)))
(push x result)))
'((not j) (a and) ((=> y b) . (c a))))
result ; => (C B Y A J)
(defun validate-wff (form)
(let ((symbols nil))
(do-symbols-in-sexp #'(lambda (x)
(if (and (not (member x '(not and or =>)))
(not (member x symbols)))
(push x symbols))) form)
(dolist (ttable (make-truth-tables (length symbols)) t)
(if (not (eval-wff form (pairlis symbols ttable)))
(return (pairlis symbols ttable))))))
(defun eval-wff (form table)
(cond
((symbolp form) (cdr (assoc form table)))
((consp form)
(case (car form)
('not
(null (eval-wff (second form) table)))
('and
(and (eval-wff (second form) table)
(eval-wff (third form) table)))
('or
(or (eval-wff (second form) table)
(eval-wff (third form) table)))
('=>
(eval-wff `(or (not ,(second form)) ,(third form)) table))))))
(defun make-truth-tables (n)
(do ((i 0 (1+ i))
(num (make-list n) (addt num))
(result))
((> i (1- (expt 2 n))) result)
(push num result)))
(defun addt (num)
(cond
((eq nil (car num))
(cons t (cdr num)))
((eq t (car num))
(cons nil (addt (cdr num))))))
(defun estimate-my-turn (eval-fun tree)
(if (null (second tree))
(funcall eval-fun (car tree))
(apply #'max (mapcar #'(lambda (x)
(estimate-his-turn eval-fun x))
(cdr tree)))))
(defun estimate-his-turn (eval-fun tree)
(if (null (second tree))
(funcall eval-fun (car tree))
(apply #'min (mapcar #'(lambda (x)
(estimate-my-turn eval-fun x))
(cdr tree)))))
(defun nim (n)
(case n
(0 '(0))
(1 '(1 (0)))
(2 '(2 (1 (0))
(0)))
(t (list n
(nim (- n 1))
(nim (- n 2))
(nim (- n 3))))))
(defun estimate-my-turn-for-nim (n)
(estimate-my-turn #'(lambda (x) 1) (nim n)))
(defun estimate-his-turn-for-nim (n)
(estimate-his-turn #'(lambda (x) 1) (nim n)))
CL-USER(15): (setq hoge 1)
1
CL-USER(16): (defun s-name (x)
(symbol-name x))
S-NAME
CL-USER(17): (s-name hoge)
Error: Attempt to access the name field of 1 which is not a symbol.
[condition type: TYPE-ERROR]
Restart actions (select using :continue):
0: Return to Top Level (an "abort" restart).
1: Abort entirely from this (lisp) process.
[1] CL-USER(18): :pop
CL-USER(19):
CL-USER(19): (s-name 'hoge)
"HOGE"
CL-USER(34): (defmacro s-name (s)
`(symbol-name (quote ,s)))
S-NAME
CL-USER(35): (s-name hoge)
"HOGE"
CL-USER(36):
(defmacro my-setf (place value)
`(setf ,(symbol-value place) ,value))
(setq hoge 1)
(defun s-name (x)
(symbol-name x))
(s-name hoge) -> error