ZTJ

.io

by Zachery Jensen

F# ASCII Tree

Recently I’ve found myself interested in learning a new programming language. Well, new to me anyway.

It has been a long time since any particular language both interested me enough to make me want to learn it and simultaneously provided enough immediate utility to allow me to make use of it in my daily work.

So I’ve just begun taking some baby steps into F#. I’m primarily interested in the high degree of static analysis and type driven “safety” features it is capable of. I have also grown more and more fond of, well, non-OOP development styles over the last 5-6 years. And while I’m no functional programming zealot, I think the FP concepts better align with my own philosophy than classical OOP concepts.

So with that said, here is a terribly trivial and probably non-idiomatic implementation of drawing a “Christmas tree” in ASCII to the console.

let rec drawTreeOther layer height =
    if layer = height then
        printfn "%41s\n%41s" "xx" "xx"
    else
        let w = (layer + 1) * 2
        printfn "%s%s" (String.replicate ((80 - w) / 2) " ") (String.replicate w "x")
        drawTreeOther (layer + 1) height

And with that, we have something silly and useless and probably somehow very buggy. Enjoy!