# [clean-list] Re: Fastest shuffle with Clean?

Siegfried Gonzi siegfried.gonzi@kfunigraz.ac.at
Thu, 13 Sep 2001 09:29:08 +0200

```CaptnJamesKirk@aol.com CaptnJamesKirk@aol.com wrote:

> OK, this makes sense. However, the Clean Book never mentions the "uselect" function, and Array updates are only
> described with the "{a &" notation. Well, maybe it's mentioned in the "case studies" of Part II, which I haven't
> gotten to yet. :(

You should also consult the Clean language report and have a look at
Zoerner's CLAS library.

>> Admitedlly, uniqueness typing is somehow confusing at the beginning.

> Yes, at least for me. It seems like a small door into which I can squeeze some procedural ideas which I probably
> shouldn't.

It is for performance issues only. But I am more contented with such a
fast "semi imperative" Clean solution instead of calling a foreign C
function (as you may have it to do in Python).

>Then it appears the door is shut. My brain is still trying hard to think in the FP way.

The problem is, that we are teached, beginning with the youth,
imperative programming. But even Stroustrup admits that learning C++
will cost you about 18 month (and this under the assumption that you are
an imperative programmer: Pascal or C). In this time I assure you, you
can also master functional programming with the exception that you then
will resolve problems and not only program (as it is the case with C++).

==
shuffleArray :: *{#Int} !Int !Int -> *{#Int}
shuffleArray ar top seed
= foldl swap ar indexPairList
where
indexPairList
= zip2 [0..top] (map (\ n -> toInt (n * z)) (take top
(genRandReal seed)))

z = toReal (top + 1)

swap a (i, j)
# (x, a ) = uselect a i
# (y, a ) = uselect a j
# a       = update a i y
# a       = update a j x
= a
==

Do you have execution times. According to your mail, your first version
is about a factor 2 slower than a C version. Please try it also with a
very huge array dimension (like 512*512) and compare it to C.

During testing your code, you can convince Clean to report the time
which it takes for garbage-collection. Your first version will show you
nearly zero garbage-collection. I would be interested in, what your
abovementioned second, more functional version reports as
garbage-collection.

S. Gonzi

```