1 people like it.

String palindrome

Tests if a string is a palindrome (whitespace independent)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
open System

let stringList (str:String) = List.filter(fun c -> c <> ' ') (Array.toList (str.ToCharArray()))

let headsMatch list1 list2 = 
    match list1 with 
        | h::t ->
            match list2 with
                | h1::t1 -> h = h1
                | [] -> false
        | [] -> List.isEmpty list2

let rec palindrome forward backward = 
    if headsMatch forward backward then
        if List.isEmpty forward then 
            true
        else
            palindrome (List.tail forward) (List.tail backward)
    else
        false

let isPal str1 = palindrome (stringList str1) (List.rev (stringList str1))

let x = isPal "abc ba"
namespace System
val stringList : str:String -> char list

Full name: Script.stringList
val str : String
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

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
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 c : 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 toList : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.Array.toList
String.ToCharArray() : char []
String.ToCharArray(startIndex: int, length: int) : char []
val headsMatch : list1:'a list -> list2:'a list -> bool (requires equality)

Full name: Script.headsMatch
val list1 : 'a list (requires equality)
val list2 : 'a list (requires equality)
val h : 'a (requires equality)
val t : 'a list (requires equality)
val h1 : 'a (requires equality)
val t1 : 'a list (requires equality)
val isEmpty : list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.isEmpty
val palindrome : forward:'a list -> backward:'a list -> bool (requires equality)

Full name: Script.palindrome
val forward : 'a list (requires equality)
val backward : 'a list (requires equality)
val tail : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.tail
val isPal : str1:String -> bool

Full name: Script.isPal
val str1 : String
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val x : bool

Full name: Script.x
Raw view Test code New version

More information

Link:http://fssnip.net/il
Posted:11 years ago
Author:devshorts
Tags: palindrome , strings