[clean-list] Fastest shuffle with Clean?

TAntonini tototbol@mageos.com
Fri, 14 Sep 2001 00:43:33 +0200


>John wrote
>
>>Greetings all,
>>I am new to FP and Clean, and I need some help here. A couple of
questions:
>
>Hi John,
>
>> My first question is how to implement the fastest shuffle possible in
>Clean.
>
>>shuffleArray :: *{#Int} !Int -> *{#Int}
>>shuffleArray ar top
>>    | top < 0   = ar
>>    | otherwise
>>        #! r = randomInt mod (top+1)
>>        #! rnd_elem = ar.[r]
>>        #! top_elem = ar.[top]
>>        = shuffleArray {ar & [r]=top_elem, [top]=rnd_elem} (top-1)
>
>
...
>Now, I am not sure the compiler does agree with your way of swapping the
>elements of the array. When you write :
>
>>        #! rnd_elem = ar.[r]
>>        #! top_elem = ar.[top]
>
>The array ar is no more single-threaded, there is a duplicated reference to
>it. The following swapping function does however perform the swapping of
two
>array items while keeping the array single-threaded :
...

I think it's ok because the let are strict ,so additional reference to ar is
removed before evaluating
top_elem and then before evaluating the last expression (shuffleArray)
If you remove them,or if you do :
#!(rnd_elem, top_elem ) = (ar.[r],ar.[top])
it doesn't work anymore

So,you don't have to change this part!

Thomas Antonini
mailto:antonini@pps.jussieu.fr