miércoles, 15 de julio de 2015

Translate simple program from Java to Clojure

In Java:
public static double hypot (double x, double y) {
 final double x2 = x * x;
 final double y2 = y * y;
 return Math.sqrt(x2 + y2);
}
In Clojure
(defn hypot [x y]
 (let [x2 (* x x)
  y2 (* y y)]
 (Math/sqrt (+ x2 y2))
  )
)
  The execution
(println (hypot 2 3))

martes, 14 de julio de 2015

My first code in clojure

I've finished reading Javascript The Good Parts. Yesterday a co-worker said to me that I could learn some functional language, He recommend to me Clojure son I've just started with Programming Clojure and this is my first little program:

(defn average [numbers]
 (/ 
 (apply + numbers)
 (count numbers)
 )
)

(println 
 (average [1 2 3 4 5])
)

When I was writing this little piece of code, I've remember when I was at university and I studied Lisp in IA signature.