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: 
19: 
let subs (s:string) =             
   let lim = s.Length - 1        

   [ for ii in [0..lim] do
      for jj in [0..lim] do  // can these two be combined?
         if ii < jj then
            yield s.[ii..jj] ]

let isPalindrome s =
   let s' = Array.ofSeq s
   (s' = Array.rev s')

let palindromes s =
   printf "Palindromes within \"%s\": \t" s
   s |> subs |> List.filter isPalindrome |> List.map (printf "%s ") |> ignore
   printf "\n"

palindromes "abcbcbx"
palindromes "abcba"
val subs : s:string -> string list

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
val lim : int
property System.String.Length: int
val ii : int
val jj : int
val isPalindrome : s:seq<'a> -> bool (requires equality)

Full name: Script.isPalindrome
val s : seq<'a> (requires equality)
val s' : 'a [] (requires equality)
module Array

from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.ofSeq
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val palindromes : s:string -> unit

Full name: Script.palindromes
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
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 filter : predicate:('T -> bool) -> list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.filter
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Next Version Raw view Test code New version

More information

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