13 people like it.

Palindromic dates

Today, 11. february 2011, is a palindromic day according to the European date format (day/month/year). This snippet collects all the palindromic dates until 31 dec. 9999. They are 366, a surprisingly low number.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
open System;
open System.Linq;

let isPalindrome (s : string) =
    (seq s).SequenceEqual(s.Reverse())

let rec getPalindromes (first : DateTime) last acc =
    match first with
    | n when n = last -> acc
    | n when (isPalindrome (n.ToString("ddMMyyyy"))) -> getPalindromes (first.AddDays(1.0)) last (n::acc)
    | _ -> getPalindromes (first.AddDays(1.0)) last acc

let allPalindromes = getPalindromes (new DateTime (1, 1, 1)) (new DateTime(9999, 12, 31)) []


Seq.iter (fun (d : DateTime) -> (printfn "%s" (d.ToShortDateString()))) (seq allPalindromes)

printfn "count: %d" (allPalindromes.Count())
namespace System
namespace System.Linq
val isPalindrome : s:string -> bool

Full name: Script.isPalindrome
val s : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = 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> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
(extension) Collections.Generic.IEnumerable.Reverse<'TSource>() : Collections.Generic.IEnumerable<'TSource>
val getPalindromes : first:DateTime -> last:DateTime -> acc:DateTime list -> DateTime list

Full name: Script.getPalindromes
val first : DateTime
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
val last : DateTime
val acc : DateTime list
val n : DateTime
DateTime.ToString() : string
DateTime.ToString(provider: IFormatProvider) : string
DateTime.ToString(format: string) : string
DateTime.ToString(format: string, provider: IFormatProvider) : string
DateTime.AddDays(value: float) : DateTime
val allPalindromes : DateTime list

Full name: Script.allPalindromes
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val d : DateTime
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
DateTime.ToShortDateString() : string
(extension) Collections.Generic.IEnumerable.Count<'TSource>() : int
(extension) Collections.Generic.IEnumerable.Count<'TSource>(predicate: Func<'TSource,bool>) : int
Raw view Test code New version

More information

Link:http://fssnip.net/2D
Posted:13 years ago
Author:Francesco De Vittori
Tags: sequences , lists , dates