Xref: utzoo comp.lang.prolog:3145 comp.lang.functional:416 Path: 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: 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 >>>>> 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