2025/12/11
Alternative external formats for arrays and complex numbers
Gauche now recognizes a few different external format for arrays and complex numbers. For writing, it can be selected by <write-controls> object, or print-mode in REPL.
Array literals
Gauche has been supporting srfi:25 arrays, but it does not define external representations.
Gauche uses srfi:10 #, mechanism to allow to write
arrays that can be read back, but it is not very user-frendly.
gosh$ (tabulate-array (shape 0 4 0 4) *) #,(<array> (0 4 0 4) 0 0 0 0 0 1 2 3 0 2 4 6 0 3 6 9)
Now we have this:
gosh$ (tabulate-array (shape 0 4 0 4) *)
#2a((0 0 0 0)
(0 1 2 3)
(0 2 4 6)
(0 3 6 9))
The #2a(...) notation is defined in srfi:163 Enhanced Array Literals, and in its
simplest form, it is also compatible to Common Lisp's array literals. From 0.9.16, it is
the default output format of the array.
You can also make Gauche reports the lengthes of each dimension:
gosh$ ,pm array dimensions
Current print mode:
length : 50 pretty : #t bytestring : #f
level : 10 base : 10 array : dimensions
width : 80 radix-prefix : #f complex : rectangular
string-length : 256 exact-decimal : #f
gosh$ (tabulate-array (shape 0 4 0 4) *)
#2a:4:4((0 0 0 0)
(0 1 2 3)
(0 2 4 6)
(0 3 6 9))
The reader recognizes all of those formats.
Complex literals
There was an asymmetry in input/output of complex literals. For reading, both the rectangular notation
1.4142135623730951+1.4142135623730951i and the polar notation 2@0.7853981633974483 are recognized, but
for printing, it is alyways in the rectangular notation.
Now you can choose the output format.
Gauche also extended the polar notation by adding suffix pi, e.g. 2@0.25pi to specify
the phase by the multiple of pi.
The following session shows how a complex number is printed with different print-mode:
gosh> (expt -16 1/4)
1.4142135623730951+1.4142135623730951i
gosh> ,pm polar
Current print mode:
length : 50 base : 10 exact-decimal : #f
level : 10 radix-prefix : #f array : compact
pretty : #t string-length : 256 complex : rectangular
width : 79 bytestring : #f
gosh> (expt -16 1/4)
2.0@0.7853981633974483
gosh> ,pm complex polar-pi
Current print mode:
length : 50 base : 10 exact-decimal : #f
level : 10 radix-prefix : #f array : compact
pretty : #t string-length : 256 complex : polar-pi
width : 79 bytestring : #f
gosh> (expt -16 1/4)
2.0@0.25pi
Furthermore, Gauche also supports Common-Lisp style
complex notation, #c(...). This is particulary
useful to exchange data between Gauche and CL programs.
gosh> ,pm complex vector
Current print mode:
length : 50 base : 10 exact-decimal : #f
level : 10 radix-prefix : #f array : compact
pretty : #t string-length : 256 complex : vector
width : 79 bytestring : #f
gosh> (expt -16 1/4)
#c(1.4142135623730951 1.4142135623730951)
The reader can read all the complex formats.

Post a comment