My own insert function as an exercise

I am solving 4Clojure exercise, this exercise asks you to create your own insert function. My answer:

(fn my-interpose 
  ([separator input] (my-interpose separator input nil))
  ([separator input result] 
    (if 
      (empty? input) 
      (reverse (rest result))
      (my-interpose separator (rest input) (cons separator (cons (first input) result))))))

I do these exercises to learn the language when I read the book Clojure. I would like to know an opinion about my code of people with experience in this language. Can I avoid the callback? Are there any conventions that I rarely come across with such code?

+3
source share
5 answers

You have a good right starting point :). Great job.

Starting with what you have, you can:

  • Replace the recursive call with the call recur, because, as it is written, it will fall into the stack overflow

    (defn foo [stuff]
      (dostuff ... )
      (foo (rest stuff)))
    

    becomes:

    (defn foo [stuff]
      (dostuff ...)
      (recur (rest stuff)))
    

    . :

    (map dostuff stuff)

  • for

    (for [a one-list b another-list]
      (dont-give-away-the-answer))
    
+5

, ,

(defn my-interpose [sep coll]
  (when (seq coll)
    (lazy-cat [(first coll) sep]
              (my-interpose sep (rest coll)))))

arthur , recur, , 99% .

EDIT:

,

(defn my-interpose [sep coll]
  (if-let [[first & rest] coll]
    (lazy-cat [first sep] (my-interpose sep rest))))
+3

, mapcat , :

#(rest (interleave (repeat %1) %2))

, - . Clojure noobs, .

+2

, lisp .

(defn my-interpose[sep coll]                                                                                                                                                                                                                
  (letfn [(f [c]                                                                                                                                                                                                                            
            (when-not (empty? c)                                                                                                                                                                                                            
              (cons sep (cons (first c)                                                                                                                                                                                                     
                              (f (next c))))))]                                                                                                                                                                                             
    (next (f coll))))
+1

(defn my-interpose [sep coll]
  (rest (apply concat (map #(vector sep %) coll))))

,

(defn my-interpose [sep coll]
  (rest (reduce #(conj %1 sep %2) [] coll)))

The idea is to compute a sequence like that (sep x0 sep x1 sep x2 ... sep xn), and then skip the first element to get (x0 sep x1 sep x2 ... xn).

+1
source

All Articles