miércoles, 1 de abril de 2015

My first post on Clojure

My good friend and Programming Languages adviser Chris Whealy, send me tweet saying this...


I knew then that I needed to learn Clojure...after all...Chris was the one who get me into Erlang -;)

So...I started to read this book Clojure in Action so yes...you can expect a review later on...


I have experience with Functional Programming and specially with Lisp-like languages (Racket) so I guess my Clojure experience was going to be an easy one...nope...it's being fun but not easy as I thought...and not because Clojure's learning path is hard...but just because I keep thinking that it's pretty much like Racket when indeed its not so...

Clojure is a Lisp-like language...but being based on Java (on the JVM to be exact) it has some really "weird" things that take some time to digest and assimilate...but of course...that has never discourage me...I have learned Haskell after all...so no challenge is too big enough for me -;) (Ok...maybe ASM...but I'm planning to learn Fasm one of this days...)

As always...I couldn't get myself happy by just reading the book...I needed to write some code for it...and what better than a Fibonacci generator...

fibonacci.clj
(defn fib [num a b]
 (cond 
  (and (> a 0) (> num 1))
   (concat [(+ a b)] (fib (- num 1) (+ a b) a))
  (= a 0)
   (concat (conj (conj [a] b) (+ a b)) (fib (- num 1) (+ a b) b))))

(defn showFib [num]
  (fib num 0 1))

Here's the screenshot..


Obviously...you can see that I based my code on my previous Racket code...but the only similarities that remain are the parenthesis...being a Lisp-like language...you can't expect no to use parenthesis, right? -:P

Greetings,

Blag.
Development Culture.

No hay comentarios: