[clean-list] unique record fields (mk II)

Martin Wierich martinw@cs.kun.nl
Thu, 07 Jun 2001 11:16:05 +0200


When the elements of an array should stay unique you can't select the elements
via "." or "!". E.g. in

  # element = array.[index]

the element is referenced via "element" but it is still a part of the array and
hence it became non unique. In this case you have to use the replace primitive:

  replace :: !*(a .e) !Int .e -> (.e, !*(a .e))

It selects an element from the array but overwrites the array at the indicated
position with another (unique) element.

  :: T = T Int
  :: Record = { array :: .{.T} }

  function :: *T -> *T
  function (T i) = (T (i+1))

  updateRecord :: *Record Int -> *Record
  updateRecord record=:{array} index
    # (element, array)
        = replace array index (T (-1))
      array
        = { array & [index] = function element }
    = { record & array = array }

You could also use the "updateArrElt" function from the StdLib library which
applies a function to one array element.

  updateRecord2 :: *Record Int -> *Record
  updateRecord2 record=:{array} index
    = { record & array = updateArrElt function index array }

cheers
  Martin Wierich

Ilya Shpitser wrote:
> 
> Hello,
> 
>   Thank you everyone for replying, the bit about partially applied functions
>   inheriting the uniqueness attribute from their arguments makes sense.
>   I have another question (really basic this time):
>   What is a Clean idiom for destructive updates of fields deep in some tree-like
>   data structures.
>   The example I am trying is updating an array element of an array that's a
>   field of some record with some function of that array element.