DEV Community

Ivan Guerreschi
Ivan Guerreschi

Posted on

2 1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay