This SRFI is currently in withdrawn status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-183@nospamsrfi.schemers.org
. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
This SRFI introduces the formatting procedure Fox ("format of X"), which takes one required argument and a variable number of additional arguments and returns a formatted string.
This SRFI is based on SRFI 54. The optional arguments of the original CAT procedure were divided into three groups: arguments only for the number type of <object>, arguments for all types except the number type of <object>, and arguments for all types of <object>. This complexity could confuse users.
The optional arguments of this revised SRFI are divided into two groups: arguments only for the number type of <object> and arguments for all types of <object>. This simplicity also makes <writer> able to substitute for <converter>. The <precision> actually serves as ~G of Common Lisp's FORMAT and %G of C's PRINTF. The <point> of this revised one, an additional optional argument, serves as ~F or ~E of Common Lisp's FORMAT and %F or %E of C's PRINTF. This revised CAT procedure, Fox, is for speedup of non-numeric object types. For this purpose, the optional arguments only for the number type of object integrate into a list-type argument, <list-for-number>.
Additional extensions:
It is difficult to gain a complete consensus on the design of a generic formatting procedure that performs a variety of the functions provided by C's PRINTF and Common Lisp's FORMAT.
One way is to devise a free non-sequent method that easily handles optional arguments, in contrast to the conventional fixed sequent method, in order to obtain a handy optional and functional interface.
(fox <object> [<pre-string>] ;string [<port>] ;port or boolean [<width>] ;integer [<char>] ;char [<writer>] ;procedure [<list-for-number>] ;list [<converter>] ;pair [<separator>] ;vector [<post-string>] ...) ;string
The <list-for-number> is a list whose elements are <precision>, <point>, <radix>, <sign>, and <exactness>. They are effective only for the number type of <object>.
Except for <string>s, the order of all other optional arguments does not matter. When there is a <string> or <string>s without the other optional arguments, the <string> or <string>s are <post-string>.
(fox 129.995 -10 '(1)) -> "130.0 " (fox 129.995 10 '(1)) -> " 130.0" (fox 129.995 -10. #\* '(1)) -> "**130.0***" (fox 129.995 10. #\* '(1)) -> "***130.0**" (fox 129.995 10. #\* '(2)) -> "**130.00**" (fox 4048 10 #\* '(hexadecimal)) -> "*****#xfd0" (fox 4048 10 #\* '(hexadecimal sign)) -> "****#x+fd0" (fox 4048 10 #\0 '(hexadecimal sign)) -> "#x+0000fd0" (fox 4048 10 #\0 '(hexadecimal sign) `(,string-upcase . 0)) -> "#X+0000FD0" (fox 4048 10 #\B '(hexadecimal sign) `(0 . ,string-upcase)) -> "#X+BBBBFD0" (fox 4048 10 #\Z '(hexadecimal sign) `(,string-upcase . 0)) -> "ZZZZ#X+FD0" (fox 129.995 10 '(2 sign) '("$" . 0)) -> " $+130.00" (fox 129.995 10 '(2 sign) '("$" . -3)) -> " $+130" (fox 123000000 '(floating)) -> "1.23e+8" (fox 123000000 '(5 floating)) -> "1.23000e+8" (fox 1.23456789e+20 '(fixed)) -> "123456789000000000000.0" (fox 123456789.012 '(sign) '#(",")) -> "+123,456,789.012" (fox 123456789 '(sign) '#("," 4)) -> "+1,2345,6789" (fox "abcdefg" '(sign) '#(",")) -> "abcdefg" (fox "abcdefg" '(sign) '#("::" 2)) -> "a::bc::de::fg" (fox "abcdefg" '(sign) '#("::" -2)) -> "ab::cd::ef::g" (fox '(#\a "str" s)) -> "(a str s)" (fox '(#\a "str" s) write) -> "(#\\a \"str\" s)" (fox 129.995 10. #\* '(0) '("|" . "|")) -> "**|130.|**" (fox 129.995 "|" 10. #\* '(0) "|") -> "|***130.***|" (fox 'String "^" (current-output-port) 10 #\* "$") -> ^****String$ (fox 'String "^" #t 10 #\* "$") -> ^****String$ (fox 'String "^" #f 10 #\* "$") -> "^****String$" (fox 'String "^" 10 #\* "$") -> "^****String$" (define-record-type example (make-example num str) example? (num get-num set-num!) (str get-str set-str!)) (define (record-writer object string-port) (if (example? object) (begin (display (get-num object) string-port) (display "-" string-port) (display (get-str object) string-port)) (display object string-port))) (define (record-display object string-port) (display (get-num object) string-port) (display "-" string-port) (display (get-str object) string-port)) (let ((plus 12345678.901) (minus -123456.789) (ex (make-example 1234 "ex")) (file "today.txt")) (for-each (lambda (x y) (fox x #t 10 (fox y 15 '(2) record-writer '#(","))) (fox x #t 10) (fox y #t 15 '(2) (if (example? y) record-display display) '#(",")) (newline)) (list "plus: " "minus: " "net: " "ex: " "file: ") (list plus minus (+ plus minus) ex file))) -> plus: 12,345,678.901 plus: 12,345,678.90 minus: -123,456.789 minus: -123,456.79 net: 12,222,222.112 net: 12,222,222.11 ex: 1234-ex ex: 1234-ex file: today.txt file: today.txt
The sample implementation available both in the Github repo and in this .tgz
file.
[R5RS] | Richard Kelsey, William Clinger, and Jonathan Rees: Revised(5) Report on the Algorithmic Language Scheme. http://www.schemers.org/Documents/Standards/R5Rs/ |
[R6RS] | Michael Sperber, R. Kent Dybvig, Matthew Flatt, and Anton von Straaten: Revised(6) Report on the Algorithmic Language Scheme. http://www.r6rs.org |
[SRFI 54] | Joo ChurlSoo: Formatting. http://srfi.schemers.org/srfi-54/ |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.