Request: (shuffle $List into $RandomList)

I’ve done this in code, but its ugly and probably very inefficient.

@hlship: I was trying this myself and would be interested to see your code (however ugly it may be :wink:)

Here is one way you could do it:

(remove element 1 namely $Head from [$Head | $Tail] leaving $Tail)
(remove element $N namely $Elem from [$Head | $TailIn] leaving [$Head | $TailOut])
        ($N minus 1 into $Nm1)
        (remove element $Nm1 namely $Elem from $TailIn leaving $TailOut)

(shuffle [] into [])
(shuffle $Input into [$HeadOut | $TailOut])
        (length of $Input into $N)
        (random from 1 to $N into $I)
        (remove element $I namely $HeadOut from $Input leaving $Rest)
        (shuffle $Rest into $TailOut)

Now, it is somewhat inefficient to recompute the length of the list on every iteration, so you might want to define a helper predicate with an extra parameter:

(remove element 1 namely $Head from [$Head | $Tail] leaving $Tail)
(remove element $N namely $Elem from [$Head | $TailIn] leaving [$Head | $TailOut])
        ($N minus 1 into $Nm1)
        (remove element $Nm1 namely $Elem from $TailIn leaving $TailOut)

(shuffle [] of length $ into [])
(shuffle $Input of length $N into [$HeadOut | $TailOut])
        (random from 1 to $N into $I)
        (remove element $I namely $HeadOut from $Input leaving $Rest)
        ($N minus 1 into $Nm1)
        (shuffle $Rest of length $Nm1 into $TailOut)

(shuffle $Input into $Output)
        (length of $Input into $N)
        (shuffle $Input of length $N into $Output)

Hope this helps!

Hi Linus,
Is this code:

defined more thoroughly somewhere else, or is it just meant to be a starting point?
Thanks