Discussion:
suggest an elt-setter for immutable collections (mostly <seq>)
Paul Dufresne
2005-10-28 20:18:41 UTC
Permalink
I am trying to make a small <array2d> module, as it seems goo have
no arrays yet. I was first thinking to make multi-dimensions arrays
quite like in Dylan, using tuples for specifying array size and indexs.

Well, that was mostly why I wanted to use zip for, and my need to map
a function over a zip. But now, I am trying to do a simple immutable 2d
array. Immutable, in the sense that when you want to replace an element
of the array, the array pass as an argument stay unchanged after the
call, but returns a new array with the element changed in it.
This is almost the same semantics as elt-setter, except that would be
an immutable one.

Then my suggestion:
I would suggest to replace the name of current elt-setter to elt-setter!
and
add a new elt-setter with signature: (v|<any> x|<col> k|<any>) => <col>
where the returned collection would have [x k] changed, but would leave
x unchanged (of course since x is immutable).

For completeness, here is my totally untested code up to date (that is
part
of pousse game of ICFP 1998 I am trying to write in Goo):
(use goo)

(dc <array2d> (<any>)) ; immutable 2d array ; indexs debutent a zero
(dp contenu (<array2d> => <vec>))
(dp width (<array2d> => <int>))
(dp height (<array2d> => <int>))

(dm creer-matrice ( hauteur|<int> largeur|<int> => <array2d>)
(make <array2d> contenu (fab <vec> (* hauteur largeur) )
height hauteur width largeur))

(df 2d-1d ( i|<int> j<int> ar|<array2d> => <int> )
(+ (* (ar width) i) j))

(dm obtenir-elem (i|<int> j|<int> array|<array> coord|<tup> => <any>)
( elt (array contenu) (2d-1d i j array)))

(df remplace ( val pos|<int> col|<seq> )
(elt-setter val col pos))

(export
creer-matrice ; ( hauteur|<int> largeur|<int>=> <array2d> )
obtenir-elem ; ( i|<int> j|<int> array|<array2d> => <any> ) ;index
debutent a zero
remplace ; ( newvalue|<any> i|<int> j|<int> oldar|<array2d>=>
<array2d> ) ; ar n'est pas modifier
)
--
http://www.fastmail.fm - Email service worth paying for. Try it for free
Paul Dufresne
2005-10-29 16:46:46 UTC
Permalink
I have made a simple implementation of my-elt-setter for <tup>.
Also, I have revised my code for array2d.

I have revised my <array2d> code, and have made a simple implementation
of elt-setter for <tup>, called elt-setter-tup.

; Copyright Paul Dufresne, this code is released under the same
conditions as of the Expat License
; see http://www.jclark.com/xml/copying.txt

(use goo)

(dc <array2d> (<any>)) ; immutable 2d array ; indexs debutent a zero
(dp! contenu (<array2d> => <tup>))
(dp width (<array2d> => <int>))
(dp height (<array2d> => <int>))

(dm elt-setter-tup ( newval laseq i)
(cat [laseq 0 i] (tup newval) [laseq (+ i 1) *]))

(dm creer-matrice ( hauteur|<int> largeur|<int> => <array2d>)
(new <array2d> contenu (fab <tup> (* hauteur largeur) )
height hauteur width largeur))

(dm from2dto1d ( i|<int> j|<int> ar|<array2d> => <int> )
(+ (* (width ar) i) j))

(dm obtenir-elem (i|<int> j|<int> array|<array2d> => <any>)
( elt (contenu array) (from2dto1d i j array)))

(dm remplace (newvalue|<any> i|<int> j|<int> oldar|<array2d> =>
<array2d>)
(set resultat (creer-matrice (height oldar) (width oldar)))
(set (contenu resultat)
(elt-setter-tup newvalue (contenu resultat) (from2dto1d i j oldar)))
resultat)

(export
creer-matrice ; ( hauteur|<int> largeur|<int>=> <array2d> )
obtenir-elem ; ( i|<int> j|<int> array|<array2d> => <any> ) ;index
debutent a zero
remplace ; ( newvalue|<any> i|<int> j|<int> oldar|<array2d>=>
<array2d> ) ; ar n'est pas modifier
)
--
http://www.fastmail.fm - Or how I learned to stop worrying and
love email again
Paul Dufresne
2005-11-09 02:39:14 UTC
Permalink
remplace function was wrong in my previous version.
I attached a new one, which also have a new fun to show a matrix.
--
http://www.fastmail.fm - Faster than the air-speed velocity of an
unladen european swallow
Loading...