gpt4 book ai didi

clojure - Clojure实现问题的不同解决方案

转载 作者:行者123 更新时间:2023-12-03 00:06:40 25 4
gpt4 key购买 nike

这是一个问题陈述:

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

解决方案很长,

(defn large [x y]
(if (> x y) x y))

(defn large-3 [x y z]
(if(> (large x y) z) (large x y) z))

(defn small [x y]
(if (< x y) x y))

(defn small-3 [x y z]
(if (< (small x y) z ) (small x y) z))

(defn second-largest [x y z]
(let [greatest (large-3 x y z)
smallest (small-3 x y z)]
(first (filter #(and (> greatest %) (< smallest %)) [x y z]))))

(defn square [a]
(* a a)
)

(defn sum-of-square [x y z]
(+ (square (large-3 x y z)) (square (second-largest x y z))))

只是想知道在 Clojure 中可以通过哪些不同/简洁的方法来解决这个问题。

最佳答案

(defn foo [& xs]
(let [big-xs (take 2 (sort-by - xs))]
(reduce + (map * big-xs big-xs))))

关于clojure - Clojure实现问题的不同解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2179358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com