iT邦幫忙

2025 iThome 鐵人賽

DAY 3
0
Software Development

clojure 30 days系列 第 3

clojure 30 days - day 2

  • 分享至 

  • xImage
  •  

Problem Description

You get an array of numbers, return the sum of all of the positives ones.
Example [1,-4,7,12] => 1 + 7 + 12 = 20

Note

  • keywords: rest function, recursion

Implementation

(defn positive-sum [input]
  (if (empty? input)
    0
    (+ (if (pos? (first input))
         (first input)
         0)
       (positive-sum (rest input)))))

(defn tester [input exp]
  (= (positive-sum input) exp))
  
(comment
  (tester [] 0)
  (tester [1 2 3 4 5] 15)
  (tester [1 -2 3 4 5] 13)
  (tester [-1 2 3 4 -5] 9)
  (tester [-1 -2 -3 -4 -5] 0))

Answer by others

  • keywords: filter / reduce / ->> function
(defn positive-sum
  [coll]
  (->> coll
       (filter pos?)
       (reduce +)))
; -> threading first
; ->> threading last

Further Learning

  • keywords: loop /recur
; 把 [1 2 3 4 5 6] 加總
(defn sum-loop [xs]
  (loop [remaining xs   ; 初始值:整個序列
         acc 0]         ; 初始總和是 0
    (if (empty? remaining)
      acc              ; 序列走完就回傳總和
      (recur (rest remaining)      ; 下一輪:把剩下的資料放進去
             (+ acc (first remaining)))))) ; 累加

上一篇
clojure 30 days - day 1
下一篇
clojure 30 days - 番外篇 1
系列文
clojure 30 days4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言