From: Joseph S. <ten...@ya...> - 2007-07-10 07:30:13
|
Hi, I made an error in that NIL is not of type STRING if the script is run with no args. My mistake. Here's the corrected script. Regards, Joe #!/usr/local/bin/sbcl --noinform ;; prime-example.lisp adapted from clisp forums ;;; using SBCL instead of CLISP (defun primep (x) "Predicate to test the primes of x." (let ((sqrt-x (sqrt x))) (do ((i 2 (1+ i))) ((> i sqrt-x) t) (when (eq (mod x i) 0) (return nil))))) (defun find-primes (m n) "Find and print n prime numbers greater than m." (do ((i (1+ m) (1+ i)) (found 0)) ((>= found n) t) (when (primep i) (format t "~A~%" i) (incf found)))) (defun print-usage-info () "Print usage hints for this script." (format t "Usage: primes <M> <N>~%") (format t "Description: Print first N prime numbers greter than M.~%") (format t "Note: Both arguments M and N must be integers.~%")) (defun main (sb-ext:*posix-argv*) "Main function to analyze command-line args" ;(if (null (second sb-ext:*posix-argv*)) ; script-name not given? ; more correct would be to test if args list is empty (if (null (and (third sb-ext:*posix-argv*)(fourth sb-ext:*posix-argv*))) ; an empty list would be of a wrong type here, use simple 'and' instead (print-usage-info) ;(let ((arg1 (parse-integer (first sb-ext:*posix-argv*) :junk-allowed t)) (let ((arg1 (parse-integer (third sb-ext:*posix-argv*) :junk-allowed t)) ;(arg2 (parse-integer (second sb-ext:*posix-argv*) :junk-allowed t))) (arg2 (parse-integer (fourth sb-ext:*posix-argv*) :junk-allowed t))) (if (and (integerp arg1) (integerp arg2)) (find-primes arg1 arg2) (print-usage-info))))) (compile 'find-primes) (compile 'primep) (main sb-ext:*posix-argv*) ____________________________________________________________________________________ Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games. http://sims.yahoo.com/ |