Friday, April 18, 2008

gcd.l

During my CSE321 class, the instructor showed us a "greatest common denominator" program in Python. If it weren't in Python, I would've written it in Python; however, it was, so I decided to do it in Lisp:

gcd.l

(defun gcd (x y) (if (= y 0) (x) (gcd y (mod x y))))
(setq x (first (rest si::*command-args*)))
(setq y (first (rest (rest si::*command-args*))))
(if (and x y) (progn
(setq x (parse-integer x))
(setq y (parse-integer y))
(format t "~D~%" (gcd x y))
))
% gcl -f gcd.l 128 104
8

This is the first moderately-useful Lisp program I've ever written, too.

Post a Comment