Prev: faster?
Next: Article about lisp and array languages
From: Kyle M on 2 Nov 2009 08:11 On Nov 2, 7:47 am, Vassil Nikolov <vniko...(a)pobox.com> wrote: > On Mon, 2 Nov 2009 04:14:16 -0800 (PST), Kyle M <kyle...(a)gmail.com> said: > > > Personally, when this comes up, I just avoid the iteration in format > > altogether. > > (reduce (lambda (x y) (format nil "~A~C~A" x #\tab y)) '(1 2 3)) > > How would you do the above so that it works equally well for stream > output destinations, not just when a string is produced (never mind > that it is slower in the latter case), i.e. when the first argument > to FORMAT is a parameter, and also for any format directive such as > `~S' for the elements? > > In other words, neither > > (reduce (lambda (x y) (format t "~A~C~A" x #\tab y)) '(1 2 3)) > > nor > > (reduce (lambda (x y) (format nil "~S~C~S" x #\tab y)) '(1 2 3)) > > produce a desired result. > > ---Vassil. > > -- > "Even when the muse is posting on Usenet, Alexander Sergeevich?" That's a good point. I have no idea how one would do that, I was just answering the question he posted.
From: Carl Taylor on 2 Nov 2009 13:29 On Nov 2, 2:00 am, mike <mpheas...(a)gmail.com> wrote: > Hi > > Formatting a list as a comma-delimited string is easy; > > (format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3" > > How do i do the same (SBCL) but with tab characters in the output? > It must be simple but I've tried a ton of different things. Use the ~n@T directive in the format string where n is an integer to specify the width of the desired tab. CL-USER 7 > (format nil "~{~a~^~5@T~}" '(1 2 3)) "1 2 3" Carl Taylor
From: Kyle M on 2 Nov 2009 13:48 On Nov 2, 1:29 pm, Carl Taylor <carltay...(a)att.net> wrote: > On Nov 2, 2:00 am, mike <mpheas...(a)gmail.com> wrote: > > > Hi > > > Formatting a list as a comma-delimited string is easy; > > > (format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3" > > > How do i do the same (SBCL) but with tab characters in the output? > > It must be simple but I've tried a ton of different things. > > Use the ~n@T directive in the format string where n is an integer > to specify the width of the desired tab. > > CL-USER 7 > (format nil "~{~a~^~5@T~}" '(1 2 3)) > "1 2 3" > > Carl Taylor The ~T is not equivalent to #\Tab. But like Madhu said earlier, it depends on your editor. Some may treat #\Tab as just some number of spaces, so it wouldn't matter anyway.
From: mike on 2 Nov 2009 16:52 Thanks everyone for the different solutions. In this case I wasnt clear enough that I'm writing something like an awk script to reformat tab-delimited input, so need an actual tab. Thanks to Vassil for pointing out the different results from (reduce ... (format nil ...)) and (reduce ... (format t ...)), I didnt know that either. In the end I guess my problem is just how to embed a tab char into the format string using an 'escape sequence'. (Embedding the actual tab char in the source isnt appealing.) This seems to work ok: (format t (concatenate 'string "~{~a~^" (string #\Tab) "~}~%") '(1 2 3)) Otherwise the ~/ is a good way to go. I thought there would have been an easier way to insert control chars into the format string; seems wierd that there is a special case for ~ %, and that recognising codes like #\Tab wouldnt be in the spec. Cheers M On Nov 3, 4:48 am, Kyle M <kyle...(a)gmail.com> wrote: > On Nov 2, 1:29 pm, Carl Taylor <carltay...(a)att.net> wrote: > > > > > On Nov 2, 2:00 am, mike <mpheas...(a)gmail.com> wrote: > > > > Hi > > > > Formatting a list as a comma-delimited string is easy; > > > > (format nil "~{~a~^,~}" '(1 2 3)) => "1,2,3" > > > > How do i do the same (SBCL) but with tab characters in the output? > > > It must be simple but I've tried a ton of different things. > > > Use the ~n@T directive in the format string where n is an integer > > to specify the width of the desired tab. > > > CL-USER 7 > (format nil "~{~a~^~5@T~}" '(1 2 3)) > > "1 2 3" > > > Carl Taylor > > The ~T is not equivalent to #\Tab. But like Madhu said earlier, it > depends on your editor. Some may treat #\Tab as just some number of > spaces, so it wouldn't matter anyway.
From: Pascal J. Bourguignon on 2 Nov 2009 17:09
mike <mpheasant(a)gmail.com> writes: > I thought there would have been an easier way to insert control chars > into the format string; seems wierd that there is a special case for ~ > %, and that recognising codes like #\Tab wouldnt be in the spec. A new line is a notion that exists on all the textual output devices of all the computers. It may be very different, eg. punching a different card, or printing a different line on a line printer, or possibly displaying a line in a different y coordinate on a glass tty, but the notion exists. On the contrary, the control character TAB doesn't exist in most encodings. (That's why #\Tab is semi-standard: you can use it only if it exists, and if it exists, it must be #\Tab ; to write a conformant program you should write something like: (defparameter *tab-char* #+ #.(cl:if (cl:ignore-errors (cl:read-from-string "#\\Tab")) '(:and) '(:or)) #\Tab #- #.(cl:if (cl:ignore-errors (cl:read-from-string "#\\Tab")) '(:and) '(:or)) #\Space) and further use *tab-char* instead of #\Tab which may not exist.), and even if it exists in the encoding used by your lisp system, nothing says that this character corresponds to a meaningful control code for the devices where you send it, so you should avoid using it anyways (the same is true of the other characters that may be defined to represent control codes). In particular, notice that ~% does not mean to insert a new line "character" in a string. #\Newline is a kludge introduced to ease the processing of multi-line strings, but when you output it, or when FORMAT encounter ~% (or ~& in the case it determines it must put a new line), no newline "character" is output. (There does not exist such a newline "character"). What lisp does is to call TERPRI. And what TERPRI does depends on the device: again it may terminate the line printing, which produce the printing of the line on a line printer. Or it may advance to the next punch card. Or it may output zero, one or more control codes (it may output zero control codes when the output external format is a variable length line format with a length, or a fixed length line format). -- __Pascal Bourguignon__ |