Prev: Flushing output
Next: Mathematica emulation
From: Cecil Westerhof on 19 Dec 2009 14:39 For a script I am writing there should be non-empty parameters. For this I made the following: (defvar *inputfile-name* (first *args*)) (defvar *messages* ()) (defvar *outputfile-name* (second *args*)) (if (not (non-empty-string *inputfile-name*)) (setq *messages* (append *messages* (list "No inputfile has been provided")))) (if (not (non-empty-string *outputfile-name*)) (setq *messages* (append *messages* (list "No outputfile has been provided")))) (if (length *messages*) (progn (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) (bye))) As far as I know there is not a function to check that a parameter is a non empty string. So I wrote a function for it: (defun non-empty-string(value) (and value (string> value ""))) Maybe it is useful to someone else also. If things could be done better, I would like to know. Also the four spaces are hard coded in: (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) I would like to do this with a parameter. So the space could be four, six, eight or any other number. Is this possible? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: joswig on 19 Dec 2009 15:01 On 19 Dez., 20:39, Cecil Westerhof <Ce...(a)decebal.nl> wrote: > For a script I am writing there should be non-empty parameters. For this > I made the following: > (defvar *inputfile-name* (first *args*)) > (defvar *messages* ()) > (defvar *outputfile-name* (second *args*)) > > (if (not (non-empty-string *inputfile-name*)) > (setq *messages* (append *messages* (list "No inputfile has been provided")))) > (if (not (non-empty-string *outputfile-name*)) > (setq *messages* (append *messages* (list "No outputfile has been provided")))) > (if (length *messages*) > (progn (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) > (bye))) (unless (non-empty-string-p ...) (push "No inputfile has been provided" *messages*)) .... (when messages (format t ...) ... ...) later use (reverse messages) > > As far as I know there is not a function to check that a parameter is a > non empty string. So I wrote a function for it: > (defun non-empty-string(value) > (and value > (string> value ""))) (and (stringp object) (zerop (length object))) > > Maybe it is useful to someone else also. If things could be done better, > I would like to know. > > Also the four spaces are hard coded in: > (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) > > I would like to do this with a parameter. So the space could be four, > six, eight or any other number. Is this possible? (dolist (message messages) (format t " ~a" message)) > > -- > Cecil Westerhof > Senior Software Engineer > LinkedIn:http://www.linkedin.com/in/cecilwesterhof
From: Kaz Kylheku on 19 Dec 2009 15:18 On 2009-12-19, Cecil Westerhof <Cecil(a)decebal.nl> wrote: > As far as I know there is not a function to check that a parameter is a > non empty string. So I wrote a function for it: > (defun non-empty-string(value) > (and value This assures that value is not nil, not that it is a string. If your idea here is to return T if the value is a string that is empty, but NIL if the object is any other object (a non-string, or a non-empty string), this is wrong. If your idea is to simply avoid calling a string function on NIL, this is pointless. Lisp is not C; we don't get a null dereference hat blows up the library if we pass NIL to it. The string> functino can take care of itself with regard to a NIL argument. > (string> value ""))) Also: (string/= value "") (zerop (length value)) > Maybe it is useful to someone else also. If things could be done better, > I would like to know. > > Also the four spaces are hard coded in: > (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) > > I would like to do this with a parameter. So the space could be four, > six, eight or any other number. Is this possible? Yes. Firstly, a fixed field width of 10 would be encoded like this: ~10a But you want that right-aligned, so we'd add a modifier: ~10@a Now, knowing where to put the field width and how to get right adjustment, how do we make the width variable? This is done by substituting the letter v: (format t "~v@a~%" 20 "foo") 'v' pulls out an argument to be used in its place, then 'a' pulls out the object to be printed. If you just want to generate that many spaces, rather than right-align something in a field, then just print the empty string: ;; fifty spaces followed by asterisk (format t "~va*~%" 50 "")
From: Cecil Westerhof on 19 Dec 2009 16:19 "joswig(a)corporate-world.lisp.de" <joswig(a)lisp.de> writes: > On 19 Dez., 20:39, Cecil Westerhof <Ce...(a)decebal.nl> wrote: >> For a script I am writing there should be non-empty parameters. For this >> I made the following: >> (defvar *inputfile-name* (first *args*)) >> (defvar *messages* ()) >> (defvar *outputfile-name* (second *args*)) >> >> (if (not (non-empty-string *inputfile-name*)) >> (setq *messages* (append *messages* (list "No inputfile has been provided")))) >> (if (not (non-empty-string *outputfile-name*)) >> (setq *messages* (append *messages* (list "No outputfile has been provided")))) >> (if (length *messages*) >> (progn (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) >> (bye))) > > (unless (non-empty-string-p ...) > (push "No inputfile has been provided" *messages*)) > > ... > > (when messages > (format t ...) > ... > ...) > > > > later use (reverse messages) That is better code. Thanks. >> As far as I know there is not a function to check that a parameter is a >> non empty string. So I wrote a function for it: >> (defun non-empty-string(value) >> (and value >> (string> value ""))) > > (and (stringp object) (zerop (length object))) That is also better. >> Also the four spaces are hard coded in: >> (format t "Program is not correctly called~%~{ ~a~%~}~%" *messages*) >> >> I would like to do this with a parameter. So the space could be four, >> six, eight or any other number. Is this possible? > (dolist (message messages) > (format t " ~a" message)) That does the same and needs more lines. It replaces: (format t "Program is not correctly called~%~{ ~a~%~}~%" (reverse *messages*)) with: (format t "Program is not correctly called~%") (dolist (message (reverse *messages*)) (format t " ~a~%" message)) So I prefer my version. It would be nice if the last line could be used like: (format t "~4s~a~%" "" message)) but that gives: "" No inputfile has been provided So that does not work. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: W. James on 19 Dec 2009 17:03
Cecil Westerhof wrote: > For a script I am writing there should be non-empty parameters. For > this I made the following: > (defvar *inputfile-name* (first args)) > (defvar messages ()) > (defvar *outputfile-name* (second args)) > > (if (not (non-empty-string *inputfile-name*)) > (setq messages (append messages (list "No inputfile has been > provided")))) (if (not (non-empty-string *outputfile-name*)) > (setq messages (append messages (list "No outputfile has been > provided")))) (if (length messages) > (progn (format t "Program is not correctly called~%~{ > ~a~%~}~%" messages) (bye))) > > As far as I know there is not a function to check that a parameter is > a non empty string. So I wrote a function for it: > (defun non-empty-string(value) > (and value > (string> value ""))) messages = [] in_file_name = ARGV.shift or messages << "Input file needed." out_file_name = ARGV.shift or messages << "Output file needed." unless messages.empty? puts "Quitting because of errors:" puts messages exit end -- |