【心得】
[& args]
(reduce + (1 2))
的錯誤看了 5min 才發現得讓 () 變成 list 才行 -> (reduce + '(1 2))
【今日湯底】
Write a function that finds the sum of all its arguments.
eg:
(sum 1 2 3) ; => 6
(sum 8 2) ; => 10
(sum 1 2 3 4 5) ; => 15
(必須通過以下測試)
(ns summation-test
(:require [clojure.test :refer :all]
[summation :refer [sum]]))
(deftest simple-tests
(is (sum 1) 1)
(is (sum 1 2) 3)
(is (sum 5 7 9) 21)
(is (sum 12 1 1 1 1) 16)
(is (sum 12 1 1 1 1 1 1) 18))
【我的答案】
(ns summation)
(defn sum [& n]
(reduce + n)
)
思路:
[& n]
可以處理不論多少參數都收在 n 裡面的作法【其他人的答案】
(ns summation)
(def sum +)