14 题以前答案一般没啥区别,略过……
#15
1 | #(* % 2) |
adereth’s solution:
1 | * 2 |
#16
1 | format "Hello, %s!" |
adereth’s solution:
1 | #(str "Hello, " % "!") |
#17
1 | '(6 7 8) |
adereth’s solution:
1 | [6 7 8] |
#18
1 | '(6 7) |
adereth’s solution:
1 | '(6 7) |
#19
1 | #(peek (vec %)) |
adereth’s solution:
1 | (comp first reverse) |
#20
1 | #(nth (reverse %) 1) |
adereth’s solution:
1 | (comp second reverse) |
#21
1 | (fn getn [ary n] |
adereth’s solution:
1 | #(first (drop %2 %1)) |
#22
1 | #(reduce (fn [a b] (inc a)) 0 %) |
adereth’s solution:
1 | (fn [sequence] (reduce (fn [acc v] (inc acc)) 0 sequence)) |
#23
1 | #(reduce (fn [rs ls] (cons ls rs)) [] %) |
adereth’s solution:
1 | (fn [s] |
#24
1 | #(reduce + %) |
adereth’s solution:
1 | apply + |
#25
1 | #(filter odd? %) |
adereth’s solution:
1 | filter odd? |
#26
1 | #(take % ((fn fib [a b] |
adereth’s solution:
1 | #(take % |
#29
1 | #(clojure.string/replace % #"[^A-Z]" "") |
adereth’s solution:
1 | (fn [s] (reduce str (filter #(Character/isUpperCase %) s))) |
#27
1 | #(= (reverse %) (seq %)) |
#28
1 | (defn cci [ls r] |
#30
1 | #((fn dis [ss l r] |
1 | #((fn dis [ss l r] |
adereth’s solution:
1 | #(map first (partition-by identity %)) |
#32
1 | #(mapcat (fn [s] [s s]) %) |
#34
1 | #((fn rng [s n r] |
adereth’s solution:
1 | (fn [start end] (take-while #(< % end) |
#35
1 | 7 |
#37
1 | "ABC" |
#38
1 | (fn [& s] (reduce (fn mx [a b] (if (> a b) a b)) s)) |
adereth’s solution:
1 | (fn [ & xs ] |
#39
1 | (fn l [a b & r] |
adereth’s solution:
1 | (fn [s1 s2] |
#40
1 | (fn [x ys] (butlast (flatten (map #(list % x) ys)))) |
adereth’s solution:
1 | #(butlast (interleave %2 (repeat %1))) |
#41
1 | (fn [ls n] |
adereth’s solution:
1 | (fn [coll n] |
#42
1 | #((fn factorial [n r] |
adereth’s solution:
1 | #(reduce * (range 1 (inc %))) |
#48
1 | 6 |
#52
1 | [c e] |
#57
1 | '(5 4 3 2 1) |
#62
1 | (fn fb ([f b] (lazy-seq (cons b (fb f (f b)))))) |
aceeca1’s solution:
1 | (fn [f x] (reductions #(%2 %1) x (repeat f))) |
注:从中学习和理解到:
- fn 给命名
name?
后也可以递归; - lazy-seq 适合做无限循环递归的懒序列;
- reductions 可以将一个数组通过某个方法做 reduce 结果为一个懒序列;
#64
1 | + |
#66
1 | (fn [a b] |
astangl’s solution:
1 | (fn |
注:从中学习和理解到:
- recur 可以递归 fn 这样的匿名函数;
- (mod x y) 当
x < y
时,返回 x ,也就是说 x/y 得 0 余 x; - 最大公约数 w 一定也可以除尽 (mod x y) 的余数;
#68
1 | '(7 6 5 4 3) |
adereth’s solution:
1 | [7 6 5 4 3] |
#71
1 | last |
#72
1 | apply + |
adereth’s solution:
1 | #(reduce + %) |
#81
1 | (fn [a b] (reduce #(if (contains? a %2) (conj %1 %2) %1) #{} b)) |
aceeca1’s solution:
1 | (comp set filter) |
- 学到的经验:#{} set 是可以当做 contains 方法来 filter 的。
#99
1 | (fn [a b] (map #(Integer/parseInt(str %)) (str (* a b)))) |
aceeca1’s solution:
1 | (comp (partial map #(- (int %) 48)) str *) |
- 学到的经验:可以组合几个方法,或者用 ->> 语法串联方法
#99
#107
1 | (fn [n] #(reduce * (repeat n %))) |
#134
1 | #(= nil (get %2 %1 0)) |
adereth’s solution:
1 | #(nil? (get %2 % true)) |
#145
1 | [1 5 9 13 17 21 25 29 33 37] |
#156
1 | #(apply merge (map (fn [k] {k %1}) %2)) |
1 | #(apply array-map (mapcat (fn [k] [k %1]) %2)) |
adereth’s solution:
1 | (fn [default values] (reduce #(assoc %1 %2 default) {} values)) |
#161
1 | #{1 2} |
#162
1 | 1 |
Another solution
Solutions to 4Clojure Easy Problems
#107
1 | (fn [n] #(reduce * (repeat n %))) |
#134
1 | #(= nil (get %2 %1 0)) |
adereth’s solution:
1 | #(nil? (get %2 % true)) |
#145
1 | [1 5 9 13 17 21 25 29 33 37] |
#156
1 | #(apply merge (map (fn [k] {k %1}) %2)) |
1 | #(apply array-map (mapcat (fn [k] [k %1]) %2)) |
adereth’s solution:
1 | (fn [default values] (reduce #(assoc %1 %2 default) {} values)) |
#161
1 | #{1 2} |
#162
1 | 1 |