O Tannenbaum

December 24, 2013

; o tannenbaum

; rand -- returns integer on range 0 .. 2^32-1
(define rand ; marsaglia linear congruential method
  (let* ((a 69069) (c 1234567) (m 4294967296)) ; due to knuth
  (lambda () (set! seed (modulo (+ (* a seed) c) m)) seed)))

; seed -- number of seconds since the Unix epoch NON-PORTABLE
; please report ports to other Scheme systems to the author
; uncomment exactly one of the next four options
(define seed (time-second (current-time))) ; chez
; (define seed (current-seconds)) ; racket/chicken
; (define seed (current-time)) ; guile
; (define seed (inexact->exact (round
;   (time->seconds (current-time))))) ; gambit

(define (center width str)
  (let* ((len (string-length str))
         (indent (quotient (- width len) 2)))
    (display (make-string indent #\space))
    (display str) (newline)))

(define (row n)
  (let loop ((n n) (str ""))
    (if (zero? n) str
      (loop (- n 1)
            (string-append
              (if (< (rand) 536870912) "*" "O")
              " " str)))))

(define (tannenbaum)
  (center 60 "*")
  (do ((n 2 (+ n 1))) ((< 20 n))
    (center 60 (row n)))
  (do ((n 1 (+ n 1))) ((< 3 n))
    (center 60 "XXX")))

> (tannenbaum)
                             *
                            * O
                           O * O
                          O O O O
                         * O O O O
                        O O O O O O
                       O O * O O O O
                      O O * O O O O O
                     O O O O O O O O *
                    O O O O O O O O O O
                   O O O O O O * * O O O
                  O O O O O O O O * O O O
                 O O O * O O O O O O O * O
                * O O * O O O O O O O O O O
               * O * O O O O * O O O O * O *
              O * O O * O O O O O O * O O O O
             O O O O O O O O O O O O O O O O O
            O O O O O O O O O O O O O O O * O O
           O O O O O O O * O O O * O O O O O O O
          O O O O O * O O O O O O O O O O O O O O
                            XXX
                            XXX
                            XXX

About these ads

Pages: 1 2

4 Responses to “O Tannenbaum”

  1. Santa's Elf said

    In haskell, a nice little christmas tree.

    http://codepad.org/14SQwTia

  2. Santa's Gopher said

    Hacked up version in Go.

    http://play.golang.org/p/uNUoCadnLT

    Merry Christmas!

  3. warrior said

    After looking at other entries I feel I should have tried to spruce it up a bit ;).

    http://codepad.org/oMKIX4Fx

  4. lmon said

    ConPHPigurable Tree:

    $rows = array(1,3,5,7,9,11,13,15,17,20,25,31,2,2,2);
    $cp = $rows;
    sort($cp);
    $wi = $cp[count($cp)-1];

    $char = “*”;
    foreach ($rows as $c) {
    $buff = round(($wi-$c)/2);
    print str_repeat ( ” ” , $buff );
    print str_repeat ( $char , $c );
    print “\n”;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 635 other followers

%d bloggers like this: