我自己介入功能作为练习 [英] my own interpose function as an exercise
问题描述
我正在解决一个4Clojure运动,这个练习要求你建立自己的插入功能。我的回答如下:
I'm resolving a 4Clojure exercise, this exercise asks you to build your own interpose function. My answer follows:
(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))))))
练习来学习语言,因为我读了一本Clojure书。我想知道关于我的语言代码的意见。我可以避免反向调用吗?有什么约定,我几乎不打破这种代码?
I'm doing these exercises to learn the language as I read a Clojure book. I will like to know the opinion about my code of people with an experience in the language. Could I avoid the reverse call? Are there any conventions I'm breaking hardly with this kind of code?
推荐答案
你有一个很好的起点:)。优秀的工作。
What you have is a good proper starting point :). Excellent work.
从你有你想要的开始:
-
用对
recur
的调用替换您的递归调用,因为写入时会触发堆栈溢出
Replace your recursive call with a call to
recur
because as written it will hit a stack overflow
(defn foo [stuff]
(dostuff ... )
(foo (rest stuff)))
变成:
(defn foo [stuff]
(dostuff ...)
(recur (rest stuff)))
吹堆。这通常会变成:
(map dostuff stuff)
将完全替换为函数
(for [a one-list b another-list]
(dont-give-away-the-answer))
这篇关于我自己介入功能作为练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!