unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Reader syntax for accessing arrays
@ 2011-08-22 23:17 Panicz Maciej Godek
  2011-08-23  8:10 ` Johan Hidding
  2012-01-09 15:39 ` Andy Wingo
  0 siblings, 2 replies; 8+ messages in thread
From: Panicz Maciej Godek @ 2011-08-22 23:17 UTC (permalink / raw)
  To: guile-user

Hi,
I've noticed that one of the biggest inconveniences of lisp is a very
clumsy way of accessing arrays.
Having to write
(array-set! a (* (array-ref a i j) 2) i j))
seems to be unnecessarily prolix, for in C, language designed
specifically to access arrays,
the same operation could be written as
a[i][j] *= 2;

Indeed, LISP is intended for processing lists, but there are certain
tasks where dealing with
arrays is inevitable. So perhaps it would be a good idea to use square
brackets, as it is
done in C, to access arrays, so that
[a i j]
could be understood by the interpreter as
(ref-array a i j)
where ref-array is the appropriate getter with setter.

Therefore I wonder how could this functionality be implemented in
guile, or, preferably,
in generic R^5RS. [I've heard that R^6RS makes no distinction between [] and ()]

Regards
Maciek



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-22 23:17 Reader syntax for accessing arrays Panicz Maciej Godek
@ 2011-08-23  8:10 ` Johan Hidding
  2011-08-23 18:03   ` Panicz Maciej Godek
  2012-01-09 15:39 ` Andy Wingo
  1 sibling, 1 reply; 8+ messages in thread
From: Johan Hidding @ 2011-08-23  8:10 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 1341 bytes --]

Hi Maciej,
I've been thinking among the same lines. At the same time this syntax could
be used to access members of a 'struct' or 'class' object. It should be
relatively simple to write a small interpreter (or maybe a pre-compiler).
Otherwise you could make a wrapper around the array in the form of a
closure. In that case, your example could be written
(*=! (a i j) 2)
Cheers, Johan

2011/8/23 Panicz Maciej Godek <godek.maciek@gmail.com>

> Hi,
> I've noticed that one of the biggest inconveniences of lisp is a very
> clumsy way of accessing arrays.
> Having to write
> (array-set! a (* (array-ref a i j) 2) i j))
> seems to be unnecessarily prolix, for in C, language designed
> specifically to access arrays,
> the same operation could be written as
> a[i][j] *= 2;
>
> Indeed, LISP is intended for processing lists, but there are certain
> tasks where dealing with
> arrays is inevitable. So perhaps it would be a good idea to use square
> brackets, as it is
> done in C, to access arrays, so that
> [a i j]
> could be understood by the interpreter as
> (ref-array a i j)
> where ref-array is the appropriate getter with setter.
>
> Therefore I wonder how could this functionality be implemented in
> guile, or, preferably,
> in generic R^5RS. [I've heard that R^6RS makes no distinction between []
> and ()]
>
> Regards
> Maciek
>
>

[-- Attachment #2: Type: text/html, Size: 1735 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-23  8:10 ` Johan Hidding
@ 2011-08-23 18:03   ` Panicz Maciej Godek
  2011-08-24 10:51     ` Ludovic Courtès
  0 siblings, 1 reply; 8+ messages in thread
From: Panicz Maciej Godek @ 2011-08-23 18:03 UTC (permalink / raw)
  To: Johan Hidding; +Cc: guile-user

> Hi Maciej,
> I've been thinking among the same lines. At the same time this syntax could
> be used to access members of a 'struct' or 'class' object. It should be
> relatively simple to write a small interpreter (or maybe a pre-compiler).

I thought that maybe it could be achieved using the guile-reader by
Ludovic, but I don't know if this library is still supported by guile (according
to the savannah web site, "it requires guile 1.8.x", but git logs suggest that
it should go with 1.9.x and 2.0.x as well). I am using 1.8 series, and I've
had some trouble with compiling it with gnu lightning, so I didn't even
manage to test it yet.

> Otherwise you could make a wrapper around the array in the form of a
> closure. In that case, your example could be written
> (*=! (a i j) 2)

Oh, that's a very good idea (at least for now).
Thanks!
M.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-23 18:03   ` Panicz Maciej Godek
@ 2011-08-24 10:51     ` Ludovic Courtès
  2011-08-24 18:45       ` Panicz Maciej Godek
  0 siblings, 1 reply; 8+ messages in thread
From: Ludovic Courtès @ 2011-08-24 10:51 UTC (permalink / raw)
  To: guile-user

Hi,

Panicz Maciej Godek <godek.maciek@gmail.com> skribis:

>> Hi Maciej,
>> I've been thinking among the same lines. At the same time this syntax could
>> be used to access members of a 'struct' or 'class' object. It should be
>> relatively simple to write a small interpreter (or maybe a pre-compiler).
>
> I thought that maybe it could be achieved using the guile-reader by 
> Ludovic, but I don't know if this library is still supported by guile (according
> to the savannah web site, "it requires guile 1.8.x", but git logs suggest that
> it should go with 1.9.x and 2.0.x as well). I am using 1.8 series, and I've
> had some trouble with compiling it with gnu lightning, so I didn't even
> manage to test it yet.

It roughly works with 2.0, but only with Latin-1 text.

>> Otherwise you could make a wrapper around the array in the form of a
>> closure. In that case, your example could be written
>> (*=! (a i j) 2)

You could even write macros for this.  I would actually find it more
elegant than additional syntax.

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-24 10:51     ` Ludovic Courtès
@ 2011-08-24 18:45       ` Panicz Maciej Godek
  2011-08-24 19:52         ` Johan Hidding
  0 siblings, 1 reply; 8+ messages in thread
From: Panicz Maciej Godek @ 2011-08-24 18:45 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-user

Hello,

>>> Otherwise you could make a wrapper around the array in the form of a
>>> closure. In that case, your example could be written
>>> (*=! (a i j) 2)
>
> You could even write macros for this.  I would actually find it more
> elegant than additional syntax.

The *=! operator could be easily created using `define-syntax'. But that's
not the issue. The most important part is array indexing (and -- as Johan
suggests -- accessing object's members). I think it would be nice to write
[a 5] instead of (array-ref a 5), and [o 'slot] instead of (slot-ref o 'slot)

That would be a fairly simple extension of syntax, I suppose.

I don't know how one could achieve this using macros. Of course, it's
possible to wrap the arrays and objects around with closures, as Johan
pointed out [and similarly to srfi-100 `define-lambda-object'], but this
has some other disadvantages.

Best regards
M.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-24 18:45       ` Panicz Maciej Godek
@ 2011-08-24 19:52         ` Johan Hidding
  2011-08-26 21:02           ` Panicz Maciej Godek
  0 siblings, 1 reply; 8+ messages in thread
From: Johan Hidding @ 2011-08-24 19:52 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: Ludovic Courtès, guile-user

Hi,
I think it is a question of philosophy. The suggested [] syntax
contains the scent of both infix notation (would [7 '* 6] work?) and
work on arrays in stead of lists (also in the case of object member
reference; the most straight forward implementation would be having a
memory offset assigned to the member). Both concepts are foreign to
the minimal nature of the scheme philosophy. To top it, getting both
concepts into a rigidly defined syntax, would require a form of
currying. The resulting language would be much more efficient for
doing scientific/numerical work, but I don't know if it's really worth
the trouble.
Cheers, Johan

2011/8/24 Panicz Maciej Godek <godek.maciek@gmail.com>:
> Hello,
>
>>>> Otherwise you could make a wrapper around the array in the form of a
>>>> closure. In that case, your example could be written
>>>> (*=! (a i j) 2)
>>
>> You could even write macros for this.  I would actually find it more
>> elegant than additional syntax.
>
> The *=! operator could be easily created using `define-syntax'. But that's
> not the issue. The most important part is array indexing (and -- as Johan
> suggests -- accessing object's members). I think it would be nice to write
> [a 5] instead of (array-ref a 5), and [o 'slot] instead of (slot-ref o 'slot)
>
> That would be a fairly simple extension of syntax, I suppose.
>
> I don't know how one could achieve this using macros. Of course, it's
> possible to wrap the arrays and objects around with closures, as Johan
> pointed out [and similarly to srfi-100 `define-lambda-object'], but this
> has some other disadvantages.
>
> Best regards
> M.
>
>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-24 19:52         ` Johan Hidding
@ 2011-08-26 21:02           ` Panicz Maciej Godek
  0 siblings, 0 replies; 8+ messages in thread
From: Panicz Maciej Godek @ 2011-08-26 21:02 UTC (permalink / raw)
  To: Johan Hidding; +Cc: Ludovic Courtès, guile-user

Howdie,

I would agree that specifying a rigid behavior for [] and {} brackets
is pretty far away from the minimalistic spirit of scheme. However,
section 7.1.1 of R5RS specification states, that characters [, ], {, }
and | are "reserved for further extension of the language".
(R6RS, on the other hand, makes a step backward, and indeed
specifies a trivial rigid behavior for []).

I eventually went without making a closure, and defined the
following syntax:

(define-syntax [
  (syntax-rules (])
    (([ v i ])
     (vector-ref v i))))

It has three disadvantages. Firstly, it still needs the additional
surrounding parentheses, secondly it requires spaces between
the adjacent symbols, and thirdly it is not R5RS compliant, due
to the reason stated above (and hence it doesn't work with
various scheme implementations).

I remember that I once used the infix module written by Daniel
Skarda, but it used the quite non-portable read-hash-extend and
I guess it is no longer shipped with guile.

I believe that there must be a way to utilize the square and
curly brackets that would suit the scheme philosophy. Perhaps
the bigloo's read mechanism is close to that way, as it is
conceptually simple, makes scheme more self-documentable
and allows it to remain compatible with the behavior of []
in R6RS, while enabling the programmer to specify new behavior
for it.

Best regards,
Maciek



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Reader syntax for accessing arrays
  2011-08-22 23:17 Reader syntax for accessing arrays Panicz Maciej Godek
  2011-08-23  8:10 ` Johan Hidding
@ 2012-01-09 15:39 ` Andy Wingo
  1 sibling, 0 replies; 8+ messages in thread
From: Andy Wingo @ 2012-01-09 15:39 UTC (permalink / raw)
  To: Panicz Maciej Godek; +Cc: guile-user

On Tue 23 Aug 2011 01:17, Panicz Maciej Godek <godek.maciek@gmail.com> writes:

> (array-set! a (* (array-ref a i j) 2) i j))
> seems to be unnecessarily prolix, for in C, language designed
> specifically to access arrays,
> the same operation could be written as
> a[i][j] *= 2;

With the appropriate macrology, it could look different:

  (with-array-cells ((x (array-ref a i j)))
    (set! x (* x 2)))

Seems OK to me.  There are other possibilities.  The macro:

  (define-syntax with-array-cells
    (syntax-rules (array-ref)
      ((_ ((sym (array-ref a idx ...)) ...) body body* ...)
       (let-syntax ((sym (identifier-syntax
                           (var (array-ref a idx ...))
                           ((set! var val) (array-set! a val idx ...))))
                    ...)
         body body* ...))))
       
Regards,

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-01-09 15:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-22 23:17 Reader syntax for accessing arrays Panicz Maciej Godek
2011-08-23  8:10 ` Johan Hidding
2011-08-23 18:03   ` Panicz Maciej Godek
2011-08-24 10:51     ` Ludovic Courtès
2011-08-24 18:45       ` Panicz Maciej Godek
2011-08-24 19:52         ` Johan Hidding
2011-08-26 21:02           ` Panicz Maciej Godek
2012-01-09 15:39 ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).