Discussion:
Array constructor in Guile?
(too old to reply)
Axel Reichert
2024-04-18 06:02:29 UTC
Permalink
Hello,

Guile has a literal syntax for vectors

#(1 2 3)

so that

(vector-ref #(1 (+ 2 3) 4) 1)

returns

(+ 2 3)

but also a "constructor"

(vector 1 (+ 2 3) 4)

so that

(vector-ref (vector 1 (+ 2 3) 4) 1)

returns

5

There is a literal syntax for arrays, too,

#2((2 1) (5 6))

so that

(array-ref #2((2 1) (5 6)) 1 1)

returns

6

But I did not find an array "constructor" syntax that allows me to
create an array such as

#2((2 1) ((+ 2 3) 6))

resulting in

#2((2 1) (5 6))

as above. How can I then create a "calculated" array, apart from filling
an array with 0 first

(define m (make-array 0 2 2))

and then changing every element with

(array-set! m 2 0 0)
(array-set! m 1 0 1)
(array-set! m (+ 2 3) 1 0)
(array-set! m 6 1 1)

?

Common Lisp's hyperspec show

(make-array '(4 2 3) :initial-contents
'(((a b c) (1 2 3))
((d e f) (3 1 2))
((g h i) (2 3 1))
((j k l) (0 0 0))))

as an example.

Pointers appreciated!

Axel
Lawrence D'Oliveiro
2024-04-18 07:33:44 UTC
Permalink
Post by Axel Reichert
But I did not find an array "constructor" syntax that allows me to
create an array such as
#2((2 1) ((+ 2 3) 6))
resulting in
#2((2 1) (5 6))
(list->array 2 `((2 1) (,(+ 2 3) 6)))
Axel Reichert
2024-04-18 17:51:16 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Axel Reichert
But I did not find an array "constructor" syntax that allows me to
create an array such as
#2((2 1) ((+ 2 3) 6))
resulting in
#2((2 1) (5 6))
(list->array 2 `((2 1) (,(+ 2 3) 6)))
Great, thanks, did not see this idea.

Best regards

Axel
Stefan Monnier
2024-04-19 19:08:04 UTC
Permalink
Post by Axel Reichert
#2((2 1) ((+ 2 3) 6))
Hmm... I see they implemented quasiquotation for vectors but not
for arrays:

scheme@(guile-user)> `#(1 2 ,(+ 3 4))
$1 = #(1 2 7)
scheme@(guile-user)> `#2((1 2) (,(+ 3 4) 5))
$2 = #2((1 2) ((unquote (+ 3 4)) 5))
scheme@(guile-user)>

I suggest you file a feature request for that.


Stefan

Loading...