2 people like it.

# Conway sequence

Generate a Conway "look and say" sequence. Each sequence element is generated by reading the previous element from left to right and saying how many repeats of each item there are. Eg. 1211-> 111221 (ie. "one one, one two, two ones").

 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: /// Get the next in a Conway look-and-say sequence (sequence A005150 in OEIS). let Next (seed : string) = seq { let prev, count = ref ((char)0), ref(0) let len = seed.Length for i in [0..len-1] do let c = seed.[i] if i = 0 then prev := c if (c <> !prev) then yield (!count), !prev count := 0 prev := c if (i = len-1) then yield (!count)+1, !prev else count := !count + 1 } |> Seq.map (fun (count, c) -> sprintf "%i%c" count c) |> Seq.fold (fun acc elem -> sprintf "%s%s" acc elem) "" /// Create a Conway look-and-say sequence (sequence A005150 in OEIS) starting /// with a given initial value. (Uses strings rather than ints to avoid quickly /// overflowing.) let ConwaySeq (first : string) = Seq.unfold (fun seed -> Some(seed, Next seed)) first // Examples: // > ConwaySeq "1" |> Seq.take 5 |> List.ofSeq;; // val it : string list = ["1"; "11"; "21"; "1211"; "111221"] // > ConwaySeq "A005150" |> Seq.take 5 |> List.ofSeq;; // val it : string list = // ["A005150"; "1A2015111510"; "111A1210111531151110"; // "311A1112111031151321153110"; "13211A311231101321151113122115132110"]
val Next : seed:string -> string

Full name: Script.Next

Get the next in a Conway look-and-say sequence (sequence A005150 in OEIS).
val seed : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val prev : char ref
val count : int ref
Multiple items
val ref : value:'T -> 'T ref

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

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
Multiple items
val char : value:'T -> char (requires member op_Explicit)

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

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
val len : int
property System.String.Length: int
val i : int
val c : char
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val count : int
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val acc : string
val elem : string
val ConwaySeq : first:string -> seq<string>

Full name: Script.ConwaySeq

Create a Conway look-and-say sequence (sequence A005150 in OEIS) starting
with a given initial value. (Uses strings rather than ints to avoid quickly
overflowing.)
val first : string
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
union case Option.Some: Value: 'T -> Option<'T>

### More information

 Link: http://fssnip.net/fe Posted: 8 years ago Author: Kit Eason Tags: sequences