0

I know this example is trivial because I think there is a clojure library function that will do this, that is not the point.

I have the following code

(defn makelistres [a b res]
  (if (= a b) res (makelistres a (dec b) (conj res b))))

(defn makelist [a b]
  (makelistres a b [])
)

Is there a way to do the same effect without having to pass the list as a parameter? Such as throwing it on the stack

Such as

(defn test [a b]
  (if (= a b) 0 (+ 1 (test a (dec b))))
)

Don't know if the parenthesis match up, as I wrote this in this text box, but you get the point.

4
  • It isn't clear to me what you mean, but you can certainly avoid having a res parameter by using a loop instead. if you are instead concerned about recursive calls consuming stack, recur can avoid this. Commented Feb 10, 2016 at 20:09
  • Yes the ultimate goal is to avoid the res parameter, is there a way to do so without loop? Commented Feb 10, 2016 at 20:14
  • What is it your makelistres function does? I'm sure it works, but I'd just like to know what to expect when running it at the REPL. Commented Feb 10, 2016 at 20:33
  • i guess it s like (vec (range b a -1)) but with stack overflow if a > b Commented Feb 10, 2016 at 20:48

2 Answers 2

4

is it an option to add one more arity to a function, which will then call the function with additional arg (empty vector) ?

like this:

(defn makelist
  ([a b] (makelist a b []))
  ([a b res]
    (if (== a b) res (makelist a (dec b) (conj res b)))))

in repl:

user> (makelist 1 10)
[10 9 8 7 6 5 4 3 2]

if not, the loop will do:

(defn makelist [a b]
  (loop [a a b b res []]
    (if (== a b)
      res
      (recur a (dec b) (conj res b)))))

or like this (since a is not changed):

(defn makelist [a b]
  (loop [b b res []]
    (if (== a b)
      res
      (recur (dec b) (conj res b)))))

but yeah, there is a lib function for that: (range 10 1 -1)

Sign up to request clarification or add additional context in comments.

Comments

-2

John

There are a couple of ways:

(def res [1,2,3,4,5])

However this requires a priori definition of the data before calling makelistres. There are ways to do this.

(def res-val (atom [])

This is more flexible using atom based functions to set and retrieve programmatically. However; you are using a mutable (stateful) object. Your ideology may shun this!

Because you are changing the content of res the atom approach seems more suited. You would basically set the value of the res (using swap) item from makelist and derefing (e.g. @res) it or modifying it in the recursion of makelistres.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.