Prev: Flushing output
Next: Mathematica emulation
From: Pascal J. Bourguignon on 19 Dec 2009 16:57 Cecil Westerhof <Cecil(a)decebal.nl> writes: > For a script I am writing there should be non-empty parameters. For this > I made the following: > [...] > (if (length *messages*) 0 is true. Any non NIL is true in lisp. -- __Pascal Bourguignon__ http://www.informatimago.com/ WARNING: This product warps space and time in its vicinity.
From: Cecil Westerhof on 19 Dec 2009 16:59 Kaz Kylheku <kkylheku(a)gmail.com> writes: > 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. My idea was to only return true when the value is a non-empty string. I thought the function equal takes care of objects that are not a string, except nil. (But I was proven wrong. Not a big problem in my case, because the value is always a string or nil.) When evaluating: (string> nil "") CL gives 0 and in CL that is true. But someone else already gave me a better solution: (and (stringp value) (zerop (length object))) >> 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 "") In this case I always want to have four spaces, so I can keep the compact form: (format t "Program is not correctly called~%~{ ~a~%~}" (reverse *messages*)) When I want to have the possibility to change the whitespace I have to go for this variant then: (format t "Program is not correctly called~%") (loop for message in (reverse *messages*) do (format t "~va~a~%" 4 "" message)) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Cecil Westerhof on 20 Dec 2009 00:50 Cecil Westerhof <Cecil(a)decebal.nl> writes: >> 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 "") > > In this case I always want to have four spaces, so I can keep the > compact form: > (format t "Program is not correctly called~%~{ ~a~%~}" (reverse *messages*)) > > When I want to have the possibility to change the whitespace I have to > go for this variant then: > (format t "Program is not correctly called~%") > (loop for message in (reverse *messages*) do > (format t "~va~a~%" 4 "" message)) I found a solution. Because it is useful in another sense, I start a new thread 'init function'. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Thomas A. Russ on 21 Dec 2009 14:36 "joswig(a)corporate-world.lisp.de" <joswig(a)lisp.de> writes: > > (unless (non-empty-string-p ...) > (push "No inputfile has been provided" *messages*)) > ... > > (when messages > (format t ...) > ... > ...) > > later use (reverse messages) Or be even cleverer and test the parameters in reverse order in the code. That way the set of messages gets constructed so that the final result is the order in which you want to print them. > > 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)) And to use a parameter, you can use some other more advanced FORMAT options. First of all, the ~T directive could be used (Hyperspec 22.3.6.1), together with the "v" parameter substitution item (22.3). Then you could have something like: (let ((indent 3)) (dolist (message messages) (format t "~vT~A" indent message))) -- Thomas A. Russ, USC/Information Sciences Institute
From: Thomas A. Russ on 21 Dec 2009 14:39
Cecil Westerhof <Cecil(a)decebal.nl> writes: > When I want to have the possibility to change the whitespace I have to > go for this variant then: > (format t "Program is not correctly called~%") > (loop for message in (reverse *messages*) do > (format t "~va~a~%" 4 "" message)) You can do better by just using the ~T directive instead of formatting the empty string into a 4 character wide field. And using ~T also makes it clear what you are doing. -- Thomas A. Russ, USC/Information Sciences Institute |