Skip to content
Discussion options

You must be logged in to vote

I would normally use var and set for this use-case

(var g nil)

(defn f [n]
  (if (zero? n)
    "f-done"
    (g (dec n))))

(set g
 (fn [n]
   (if (zero? n)
     "g-done"
     (f (dec n)))))

(print (f 10))

You can make it cleaner by defn-ing g followed by a set to that private symbol, like so:

(var g nil)

(defn f [n]
  (if (zero? n)
    "f-done"
    (g (dec n))))

(defn- g* [n]
  (if (zero? n)
    "g-done"
    (f (dec n))))

(set g g*)

(print (f 10))

Replies: 3 comments 19 replies

Comment options

You must be logged in to vote
8 replies
@CosmicToast
Comment options

@pepe
Comment options

@nstgc
Comment options

@ianthehenry
Comment options

@CosmicToast
Comment options

Answer selected by nstgc
Comment options

You must be logged in to vote
2 replies
@CosmicToast
Comment options

@sogaiu
Comment options

Comment options

You must be logged in to vote
9 replies
@nstgc
Comment options

@nstgc
Comment options

@bitcompost
Comment options

@bakpakin
Comment options

@sogaiu
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
8 participants