DEV Community

Ivan Guerreschi
Ivan Guerreschi

Posted on

Example recursion in Clojure

Recursion of the perimeter of a square

Given one side find the perimeter of each square
f(n) = 4 * n + f(n - 1)

(ns recursion.core
  (:gen-class))

(defn -main
  [& args]  
  (defn f [n]
    (if (= n 1)
      4
      (+ (* 4 n) (f (dec n)))))
  (println (f 4)))

Top comments (0)