summaryrefslogtreecommitdiff
path: root/gcd.lisp
blob: 4bf5840fa17da714bbb1e8d13bbaf8d58beeac8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
; SYNOPSIS:
; # clisp gcd.lisp 11 22 33 121
; # sbcl --script gcd.lisp 121 22 33
;

(defun gcd2 (a b)
  (if (= b 0)
    a 
    (gcd2 b (mod a b))))

(defun gcdn (&rest numbers)
  (reduce #'gcd2 (rest numbers)
          :initial-value (first numbers)))

; 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*
   nil))

(write (apply #'gcdn 
              (map 'list #'parse-integer (program-args))
              ))
(fresh-line)