1 people like it.

palindromes

Find every substring that is a palindrome. A bit lazier than the original.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let subs (s : string) =
   match s.Length with
   | 0 -> Seq.empty
   | 1 -> Seq.singleton [|s.[0]|]
   | _ ->
      let sub' n = Seq.windowed n s
      Seq.unfold (fun n -> if n <= s.Length then Some(sub' n, n+1) else None) 2
      |> Seq.collect id

let isPalindrome (s : 'a[]) =
   let l = s.Length-1
   seq { 0..(l+1)/2 } |> Seq.forall (fun i -> s.[i] = s.[l-i])

let palindromes s =
   s
   |> subs
   |> Seq.filter isPalindrome
   |> Seq.map (fun x -> System.String x)
val subs : s:string -> seq<char []>

Full name: Script.subs
val s : 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
property System.String.Length: int
module Seq

from Microsoft.FSharp.Collections
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
val singleton : value:'T -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.singleton
val sub' : (int -> seq<char []>)
val n : int
val windowed : windowSize:int -> source:seq<'T> -> seq<'T []>

Full name: Microsoft.FSharp.Collections.Seq.windowed
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>
union case Option.None: Option<'T>
val collect : mapping:('T -> #seq<'U>) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.collect
val id : x:'T -> 'T

Full name: Microsoft.FSharp.Core.Operators.id
val isPalindrome : s:'a [] -> bool (requires equality)

Full name: Script.isPalindrome
val s : 'a [] (requires equality)
val l : int
property System.Array.Length: int
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 forall : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.forall
val i : int
val palindromes : s:string -> seq<System.String>

Full name: Script.palindromes
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val x : char []
namespace System
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit

More information

Link:http://fssnip.net/4s
Posted:12 years ago
Author:Kevin Cantu
Tags: f# , palindromes