🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1164

Re: shuffling a list

#1164
From: Tim Lambert
Date: Thu, 06 Sep 1990 13:16
25 lines
1284 bytes
>>>>> On 5 Sep 90 16:58:26 GMT, quale@saavik.cs.wisc.edu (Douglas E. Quale) said:

> What is the best way to shuffle a list in a functional language?

In Miranda:

Riffle shuffle (as in a pack of cards) a list

> riffle:: [*] -> [*]
> riffle xs = riffle_merge (take half xs) (drop half xs)
>             where half = #xs div 2

Riffle merge two lists.  Take elements alternately from each
list to form the new list.

> riffle_merge (x:xs) (y:ys) = x:y:riffle_merge xs ys
> riffle_merge [] ys =  ys
> riffle_merge (x:xs) [] = x:xs

Now you can evaluate "iterate riffle [1..52]" and discover that eight
riffle shuffles put a deck back the way it started.

:-)

Tim
Technical Details
Message-ID: <LAMBERT.90Sep6231606@nankeen.spectrum.cs.unsw.oz.au>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!metro!usage.csd.unsw.oz.au!usage.csd!lambert
References: <5056@daffy.cs.wisc.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!metro!usage.csd.unsw.oz.au!usage.csd!lambert
From: lambert@spectrum.cs.unsw.oz.au (Tim Lambert)
Newsgroups: comp.lang.prolog,comp.lang.functional
Subject: Re: shuffling a list
Message-ID: <LAMBERT.90Sep6231606@nankeen.spectrum.cs.unsw.oz.au>
Date: 6 Sep 90 13:16:06 GMT
References: <5056@daffy.cs.wisc.edu>
Sender: news@usage.csd.unsw.oz.au
Followup-To: comp.lang.prolog
Organization: EE & CS, Uni of NSW, Australia
Lines: 25
In-reply-to: quale@saavik.cs.wisc.edu's message of 5 Sep 90 16:58:26 GMT