4 people like it.

ListShuffle

This simple snippet shuffles the elements of a list. It can be useful for simulations.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
open System
  
// -- | Auxiliary recursive drop function
let rec drop' i l1 p l2 =
    if List.length l2 = List.length l1 - 1 then l2
    else 
        let p = if p = i then p+1  else p 
        drop' i l1 (p+1) (List.append l2 [(List.nth l1 p)])

// -- | Removes the the nth element from list
let drop i l =
    drop' i l 0 []        

// -- | Pop one element from a specific list position
let pop i l =
    match l with
    | [] -> ([],[])
    | h :: t when i >= 0 -> 
        let e = [l.Item i]
        let ll = drop i l
        (e,ll)
    | _ -> ([],[])

// -- | Generate random number within interval a-b with seed s
let myrandom a b s =
    let r = System.Random(s)
    r.Next(a, b)

// -- | Shuffle' auxiliary
let rec shuffle' r l1 l2 =
    let len = List.length l1
    let t = pop r l1
    if len = 0 then l2
    else shuffle' (myrandom 0 (len-1) r) (snd t) (List.append l2 (fst t))  

// -- | Shuffle a list
let shuffle l =
    shuffle' (myrandom 0 ((List.length l)-1) 0) l []
   
// -- | The main entry point.
let main() = 
    let v = [1..100]
    Console.WriteLine("List Shuffle!")
    printfn "%A" (shuffle v)
    
main()
namespace System
val drop' : i:int -> l1:'a list -> p:int -> l2:'a list -> 'a list

Full name: Script.drop'
val i : int
val l1 : 'a list
val p : int
val l2 : 'a list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val length : list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.length
val append : list1:'T list -> list2:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.append
val nth : list:'T list -> index:int -> 'T

Full name: Microsoft.FSharp.Collections.List.nth
val drop : i:int -> l:'a list -> 'a list

Full name: Script.drop
val l : 'a list
val pop : i:int -> l:'a list -> 'a list * 'a list

Full name: Script.pop
val h : 'a
val t : 'a list
val e : 'a list
property List.Item: int -> 'a
val ll : 'a list
val myrandom : a:int -> b:int -> s:int -> int

Full name: Script.myrandom
val a : int
val b : int
val s : int
val r : Random
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
Random() : unit
Random(Seed: int) : unit
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
val shuffle' : r:int -> l1:'a list -> l2:'a list -> 'a list

Full name: Script.shuffle'
val r : int
val len : int
val t : 'a list * 'a list
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val shuffle : l:'a list -> 'a list

Full name: Script.shuffle
val main : unit -> unit

Full name: Script.main
val v : int list
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/pc
Posted:9 years ago
Author:Antonio Prestes GarcĂ­a
Tags: algorithms