Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Saturday, 12. December 2009


Christmas tree in F#

Filed under: Diverses — Steffen Forkmann at 12:19 Uhr

Today I had way too much time on the train 😉 , so I wrote a little functional christmas tree program:

let line width j =

  List.init

    width

    (fun i -> if abs(width/2 – i) <= j then "*" else " ")

 

let tree n =

  List.init (n/2) (line n) @        // treetop

    List.init 2 (fun _ -> line n 0) // trunk

 

let printTree =

  tree >> Seq.iter (Seq.fold (+) "" >> printfn "%s")

 

printTree 11

 

    *     
   ***    
  *****
 *******  
********* 
    *     
    * 
Tags: ,

2 Comments »

  1. Nice one! Schoene Weihnachten!

    don

    p.s. the “-” in the line

    (fun i -> if abs(width/2 – i) <= j then "*" else " ")

    is a unicode character needs to be changed to the normal "-". 🙂

    Comment by Don Syme — Saturday, 12. December 2009 um 21:28 Uhr

  2. I think I just had a flashback from the early days programming in LOGO.

    Comment by Chris Patterson — Wednesday, 16. December 2009 um 5:36 Uhr

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .