0 people like it.

Shuffle letters

Added recursive version

 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: 
open System.Text
open System
open System.Linq

let input = "elektriba"

let rec remove i l =
    match i, l with
    | 0, h::t -> t
    | i, h::t-> h::remove (i - 1) t
    | _, [] -> failwith "index out of range"

let rnd = new Random()

let shuffle (input:string) =
    let _shuffle (inp:string) =
        let mutable lst = List.ofSeq inp
        let result = Array.create inp.Length '\000'
        for i = 0 to result.Length - 1 do
            let ix = rnd.Next(lst.Length - 1)
            result.[i] <- lst.[ix]
            lst <- remove ix lst
        List.ofArray result

    match input.Length with
    | l when l < 2 -> input
    | _ -> input.[0] :: (_shuffle input.[1 .. input.Length - 2]) @ [input.[input.Length - 1]]
            |> List.map (fun (c:char) -> c.ToString())
            |> List.reduce (+)

shuffle input
namespace System
namespace System.Text
namespace System.Linq
val input : string

Full name: Script.input
val remove : i:int -> l:'a list -> 'a list

Full name: Script.remove
val i : int
val l : 'a list
val h : 'a
val t : 'a list
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val rnd : Random

Full name: Script.rnd
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
val shuffle : input:string -> string

Full name: Script.shuffle
val input : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val inp : string
val mutable lst : char 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 ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq
val result : char []
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
property String.Length: int
property Array.Length: int
val ix : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
property List.Length: int
val ofArray : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofArray
val l : int
val _shuffle : (string -> char list)
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val c : char
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = Char

Full name: Microsoft.FSharp.Core.char
Char.ToString() : string
Char.ToString(provider: IFormatProvider) : string
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.reduce
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/jN
Posted:10 years ago
Author:
Tags: shuffle