summaryrefslogtreecommitdiff
path: root/gcd.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'gcd.lisp')
-rw-r--r--gcd.lisp33
1 files changed, 17 insertions, 16 deletions
diff --git a/gcd.lisp b/gcd.lisp
index 4bf5840..8899491 100644
--- a/gcd.lisp
+++ b/gcd.lisp
@@ -1,29 +1,30 @@
; SYNOPSIS:
; # clisp gcd.lisp 11 22 33 121
+; # ecl --shell gcd.lisp 121 22 33
+; # gcl -f gcd.lisp 121 22 33
; # sbcl --script gcd.lisp 121 22 33
-;
(defun gcd2 (a b)
- (if (= b 0)
- a
- (gcd2 b (mod a b))))
+ (loop while (/= b 0) do
+ (psetq a b b (mod a b))
+ finally (return a)))
-(defun gcdn (&rest numbers)
- (reduce #'gcd2 (rest numbers)
- :initial-value (first numbers)))
+(defun gcdn (n &rest ns)
+ (reduce #'gcd2 ns :initial-value n))
-; Command line access is different on different Lisps
(defun program-args ()
(or
- #+SBCL (rest *posix-argv*)
#+CLISP *args*
- ;#+ECL (ext:command-args)
- ;#+CMU extensions:*command-line-words*
- ;#+LISPWORKS system:*line-arguments-list*
+ #+ECL (cdr ext:*unprocessed-ecl-command-args*)
+ #+GCL (cdr si::*command-args*)
+ #+SBCL (cdr *posix-argv*)
nil))
-(write (apply #'gcdn
- (map 'list #'parse-integer (program-args))
- ))
-(fresh-line)
+(defun numbers ()
+ (mapcar #'parse-integer (program-args)))
+
+(let ((ns (numbers)))
+ (when ns
+ (write (apply #'gcdn ns))
+ (fresh-line)))