0

使用される標準の L2 ノルム (ユークリッド距離) を一般化するために、Lp ノルム関数を記述しようとしています。L2ノルムをどのように記述したかを考えると、これまでに思いついたものは次のとおりです。

(defn foo [a b p]
     (reduce + (map (comp (map #(power a %) p) -) a b)))

ただし、この関数を実装しようとするたびにエラー ClassCastException が発生します。中間コードの一部は、次のコードが提供された、以前に尋ねられたベクトル内の要素を累乗するという質問からのものです。

(defn compute [exp numbers]
     (map #(power exp %) numbers))
4

2 に答える 2

1

あなたの内側(マップ):

(map #(power a %) p)

シーケンスを返しますが、それを (comp) にフィードすることはできません。「comp」は「関数合成」用です。

REPL では:

(doc comp)
clojure.core/comp
([] [f] [f g] [f g h] [f1 f2 f3 & fs])
  Takes a set of functions and returns a fn that is the composition
  of those fns.  The returned fn takes a variable number of args,
  applies the rightmost of fns to the args, the next
  fn (right-to-left) to the result, etc.

コードを小さなステップに分割し始めます。(let) フォームは非常に便利です。恥ずかしがらずに使用してください。

于 2014-01-17T15:51:41.147 に答える