node to node copy
Martin Wierich
1wierich@informatik.uni-hamburg.de
Mon, 5 Jan 1998 13:38:37 +0100 (MET)
Hello world,
Erik Zuurbier wrote, that he needs a function for making a copy of
an array.
I once wrote the function "CopyArray". It uses another function
"whileU_nu".
"whileU_nu" is a "unique" analogon to the "while"-
function in the standard environment. "whileU_nu" takes as parameters
a test-function, a transition-function and an initial value.
It performs the transition-function as long as the test function
holds. The difference to "while" is, that the initial value is
a pair of a unique and a non-unique element. (My experience is, that
it is useful to split datastructures in that way.)
"CopyArray" was designed for *unboxed* arrays. It creates an array
"new" with the same size of the "original" array. Then it uses
"whileU_nu" to copy the original array element by element into
the new array (like a "for"-loop).
CopyArray :: u:(a e) -> *(*(a e),u:(a e)) | Array a & Element e
CopyArray a
=(aCopy,a3)
where
(aSize,a2)=usize a
new=createArray aSize defaultArrayvalue
((a3,aCopy),_)=whileU_nu (\i->i<aSize) transfer ((a2,new),0)
transfer ((fromm,to),i)
=((fromm`,{to & [i]=value}),i+1)
where
(value,fromm`)=uselect fromm i
whileU_nu::(a -> Bool) ((.b,a) -> (.b,a)) (.b,a) -> (.b,a)
whileU_nu test step both=:(u,nu)
| test nu=whileU_nu test step (step both)
=both
I think, using the overloaded function "defaultArrayvalue"
restricts the use of "CopyArray" just to unboxed arrays. For other
arrays you had to pass a value to "CopyArray", which is used to
initialize the array-elements in "createArray". So I suggest to
change the first two rows to
CopyArray :: u:(a e) -> *(*(a e),u:(a e)) | Array a
CopyArray a defaultArrayvalue
The upper version of "CopyArray" worked on Clean (1.2 I think).
Unfortunately my Clean compiler doesn't work anymore. I think
the Solaris operating system has been upgraded in my university
network, or so. I get the message "can't execute the clean-compiler".
I have made a library of many of such functions, and
I wanted to post them to Nick Kallens repository, but I think,
I have to buy a PC first.
Let me finally ask: Why are arrays not allowed to contain
unique elements ?
greetings
Martin Wierich (Hamburg,Germany)