どうかく?orgでCommon Lispプログラムを書いてみた。
1 (defun center (str len &optional (padchar #\ )) 2 "If LEN is greater than the length of STR, returns a new String 3 of length LEN with STR centered and padded with PADCHAR; 4 otherwise, returns STR." 5 (let ((pad (make-string (/ (- len (length str)) 2) :initial-element padchar))) 6 (format nil "~a~a~a" pad str pad))) 7 8 (defun stars (n) 9 (- (* 2 n) 1)) 10 11 (defun pyramid (n) 12 (loop for i from 1 to n do 13 (format t "~a~%" (center (make-string (stars i) :initial-element #\*) 14 (stars n)))))
1 (require 'cl-ppcre) 2 (defun including-tax (string) 3 (cl-ppcre:regex-replace-all 4 "\\d+" string 5 (lambda (num) (format nil "~a" (floor (* (parse-integer num) 1.05)))) 6 :simple-calls t)) 7 8 ;; CL-USER> (including-tax "ダイコン150円、ハクサイ120円、ジャガイモ30円") 9 ;; "ダイコン157円、ハクサイ125円、ジャガイモ31円"
(defun lot (n m) (let ((lot (loop for i from 1 to n collect i))) (loop repeat m collect (let ((x (nth (random (length lot)) lot))) (setf lot (delete x lot)) x)))) ;; (lot 6 3) ;; (lot 9 4) ;; (lot 9999 4) (defun test-lot (lotfunc n m) (loop repeat 100 always (let ((result (funcall lotfunc n m))) (and (= (length result) m) (uniquep result))))) (defun uniquep (list) (loop for elt in list always (= 1 (count elt list)))) ;; (uniquep '(1 3 1)) ;; (uniquep '(1 2 3)) ;; (test-lot #'lot 9 4) ;; (test-lot #'lot 100 4)
(defun %rational->decimal-calc (rat) (let ((a (numerator rat)) (b (denominator rat)) ret mods) (loop do (setf a (* 10 a)) (push (floor (/ a b)) ret) (setf a (mod a b)) until (or (zerop a) (member a mods)) do (push a mods) finally (setf ret (nreverse ret))) (if (zerop a) ret (nconc (insert ret (position (floor (/ (* 10 a) b)) ret :from-end t) "{") '("}"))))) ;; insertってこれでいいのかな? ;; integer / integer -> integer な割り算は (floor (/ a b)) 以外に書けるのかな? (defun insert (list n newelt) `(,@(butlast list (- (length list) n)) ,newelt ,@(nthcdr n list))) (defun rational->decimal (rat) (multiple-value-bind (int frac) (truncate rat) (format nil "~a.~{~a~}" int (%rational->decimal-calc frac)))) ;; (rational->decimal 3/14) ;; (rational->decimal 17/14) ;; (rational->decimal 1/4)
CL-USER> (rational->decimal 3/14) "0.2{142857}" CL-USER> (rational->decimal 1/4) "0.25" CL-USER> (rational->decimal 17/14) "1.2{142857}"
ブラウザの操作ミスでダブルで投稿されてしまった。問い合わせようにも「フィードバック」から書き込もうにも500エラーorz
メールアドレスが書いてないから伝えられないよ。