Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active August 29, 2015 14:00
Show Gist options
  • Save kuanyui/955c79d033d99fb673a0 to your computer and use it in GitHub Desktop.
Save kuanyui/955c79d033d99fb673a0 to your computer and use it in GitHub Desktop.
;; loop
(defun frac (n)
(apply #'*
(loop for i from 1 to n
collect i)))
;; dotimes
;; (dotimes (x 5) ...) => 0, 1, 2, 3, 4
(defun frac (n)
(let (fin)
(dotimes (x n)
(push (1+ x) fin))
(apply #'* fin)
))
;; do
;; (do ((VAR INIT [STEP])...)
;; (END-TEST [RESULT...])
;; BODY...)
(defun frac (x)
(do ((i 2 (1+ i)) (fin 1)) ;定義變數
((> i x) fin) ;end-test和整個do的輸出結果。test一旦成立就立刻輸出fin,不再eval本體
(setq fin (* fin i)))) ;本體
;; while
(defun frac (x)
(let ((fin x))
(while (> x 1)
(setq x (1- x))
(setq fin (* fin x)))
fin))
;; recursive
(defun frac (n)
(if (<= n 1)
1
(* n (frac (1- n)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment