Source of Nothingness - format

* Menu

About | Profile | まとめ | オリジナル | xyzzy | あんてな | | buzz

* xyzzy lisp : format ~%

(format nil "~%")
"
"

(format nil "~3%")
"


"

(format nil "~0%")
""

* xyzzy lisp : format ~C

(format nil "~C" #\C-a)
"^A"
(format nil "~:C" #\C-a)
"C-a"
(format nil "~@C" #\C-a)
"#\\C-a"

* xyzzy lisp : format ~P

(format nil "cat~P" 1)
"cat"
(format nil "cat~P" 2)
"cats"
(format nil "~R cat~:P" 1)
"one cat"
(format nil "~R cat~:P" 2)
"two cats"
(format nil "cand~@P" 1)
"candy"
(format nil "cand~@P" 2)
"candies"

* 素数を求める

素直じゃない再帰・マクロ(ほぼ)無し
(defun generate-primes (limit)
  (format t "~{ ~D~}~%"
          (funcall (lambda (f c n &optional (x 3) (prime (list 2)))
                     (funcall f f c n x prime))
                   (lambda (f c n x prime)
                     (if (> x n)
                         (nreverse prime)
                       (if (funcall c c x prime)
                           (funcall f f c n (+ x 2) (cons x prime))
                         (funcall f f c n (+ x 2) prime))))
                   (lambda (c x prime)
                     (if (endp prime)
                         t
                       (if (zerop (rem x (car prime)))
                           nil
                         (funcall c c x (cdr prime)))))
                   limit)))

* 素数を求める

素直に再帰
(defun generate-primes (limit)
  (labels ((check (x prime)
             (cond ((endp prime)
                    t)
                   ((zerop (rem x (car prime)))
                    nil)
                   (t
                    (check x (cdr prime)))))
           (main (n x prime)
             (cond ((> x n)
                    (nreverse prime))
                   ((check x prime)
                    (main n (+ x 2) (cons x prime)))
                   (t
                    (main n (+ x 2) prime)))))
    (format t "~{ ~D~}~%" (main limit 3 (list 2)))))

* 素数を求める

素直にループ
(defun generate-primes (limit)
  (let ((prime nil))
    (push 2 prime)
    (do ((x 3 (+ x 2)))
        ((> x limit))
      (if (dolist (i prime t) (if (zerop (rem x i)) (return nil)))
          (push x prime)))
    (format t "~{ ~A~}~%" (nreverse prime))))

* xyzzy lisp : format ~X

(format nil "~X" #x123abc)
"123abc"
(format nil "~:X" #x123abc)
"123,abc"
(format nil "~@X" #x123abc)
"+123abc"
(format nil "~:@X" #x123abc)
"+123,abc"
(format nil "~10X" #x123abc)
"    123abc"
(format nil "~10,'*X" #x123abc)
"****123abc"
(format nil "~10,'*:@X" #x123abc)
"**+123,abc"
(format nil "~10,'*,'-,2:X" #x123abc)
"**12-3a-bc"

* xyzzy lisp : format ~D


(format nil "~D" 12345)
"12345"
(format nil "~:D" 12345)
"12,345"
(format nil "~@D" 12345)
"+12345"
(format nil "~:@D" 12345)
"+12,345"
(format nil "~10D" 12345)
"     12345"
(format nil "~10,'*D" 12345)
"*****12345"
(format nil "~10,'*:@D" 12345)
"***+12,345"
(format nil "~10,'*,'-,2:D" 12345)
"***1-23-45"

* xyzzy lisp : format ~O


(format nil "~O" #o12345)
"12345"
(format nil "~:O" #o12345)
"12,345"
(format nil "~@O" #o12345)
"+12345"
(format nil "~:@O" #o12345)
"+12,345"
(format nil "~10O" #o12345)
"     12345"
(format nil "~10,'*O" #o12345)
"*****12345"
(format nil "~10,'*:@O" #o12345)
"***+12,345"
(format nil "~10,'*,'-,2:O" #o12345)
"***1-23-45"

* xyzzy lisp : format ~B


(format nil "~B" #b11010)
"11010"
(format nil "~:B" #b11010)
"11,010"
(format nil "~@B" #b11010)
"+11010"
(format nil "~:@B" #b11010)
"+11,010"
(format nil "~10B" #b11010)
"     11010"
(format nil "~10,'*B" #b11010)
"*****11010"
(format nil "~10,'*:@B" #b11010)
"***+11,010"
(format nil "~10,'*,'-,2:B" #b11010)
"***1-10-10"

* xyzzy lisp : formatのCONTROL-STRINGとパラメータと「:」と「@」の関係

~a	4	:	@
~s	4	:	@
~d	4	:	-
~b	4	:	-
~o	4	:	-
~x	4	:	-
~r	5	:	@	:@
~p	0	:	@
~c	0	:	@
~f	5	-	@
~e	7	-	@
~g	7	-	@
~$	4	:	@	:@
~%	1	-	-
~&	1	-	-
~|	1	-	-
~~	1	-	-
~\n	0	:	@	-
~t	2	-	@	-
~*	1	:	@	-
~w	???
~?	0	-	@	-
~(	0	:	@	:@
~[	1→	:0	@0	-
~{ 	1	:	@	:@
~^	3	:	-

* xyzzy lisp : format

リファレンスで「~W」が抜けている
http://www.lispworks.com/documentation/HyperSpec/Body/22_cdc.htm

* 2006-05-23 「A・E・H・R・T」でできるすべての文字列を表示するプログラム

http://d.hatena.ne.jp/tamago_girai/20060522#p1

pushが失敗だった。訂正版。
より簡潔に。再帰が一回分増えるのがアレだったんだけど、きれいな解答ならこちらかな。
(defun a-e-h-r-t (rest &optional pool)
  (if (not rest)
      (format t "~{~A~}~%" (reverse pool))
    (dolist (x rest)
      (a-e-h-r-t (remove x rest) (cons x pool)))))
(a-e-h-r-t '(a e h r t))

* xyzzy lisp : junk/httpの利用例として

RandomNote投稿lisp
refwikiのPOST用も作ってみたり。公開はしないけど。
(ReadMore...)

* xyzzy lisp : ディレクトリ付きにしてユニークなバッファ名に

機能的にはしょぼしょぼすくりぷとさんと同等。
ひとつ上のディレクトリでやめてしまうみたいだから自分で書いてみた。
(defun buffer-name-with-directory ()
  (let* ((buffer (selected-buffer))
         (name (buffer-name buffer))
         dirs)
    (when (string-match "<[0-9]+>" name)
      (setq dirs (reverse (pathname-directory (get-buffer-file-name buffer))))
      (setq name (format nil "~A/~A" (pop dirs) (file-namestring (get-buffer-file-name buffer))))
      (while (and dirs (dolist (x (buffer-list))
                         (if (string= (buffer-name x) name)
                             (return t))))
        (setq name (format nil "~A/~A" (pop dirs) name)))
      (rename-buffer (abbreviate-display-string name 40) buffer))))
(add-hook '*find-file-hooks* 'buffer-name-with-directory)