Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

recursion - StackOverFlowError Clojure change order of list

I want to be able to take a list in clojure like

(1 2 3 4 5 6 7 8 9) 

and change the order like this:

(9 8 7 6 5 4 3 2 1)

I could not find a built-in function for (inverting? Is that a good term?) turning lists back to front or whatever you wanna call the action, if there is I'd be very thankful if someone pointed that out since creating this function is not my goal it's just a step I need to pass to do what I really want to do. Anyway I wrote a function that should do it but I get this error:

StackOverflowError   clojure.core/seq (core.clj:133)

SOLVED: built-in function called reverse solves my problem, thanks!

    (defn turn-list
     ([l]
        (turn-list (rest l) (list (first l))))
     ([l1 l2]
        (if (empty? l1)
           l2
           (turn-list (conj l1 (first l2)) (rest l2)))))
question from:https://stackoverflow.com/questions/65598913/stackoverflowerror-clojure-change-order-of-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...