Raising elements in a vector to a power

2019-09-18 10:18发布

I am trying to input a vector and parameter p, which in turn should raise each element of the vector to the power p. So far I have tried mapping the numeric tower function power, but that has proved unsuccessful. What would be the easiest way to raise each element of a vector to a power p?

(defn p' [x p] 
        (map power x p))

2条回答
【Aperson】
2楼-- · 2019-09-18 10:34

You need something like:

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

For more information, type the following in your REPL:

(doc map)
查看更多
趁早两清
3楼-- · 2019-09-18 10:34

To expand on Chiron's answer, you could also do with partial application:

(defn compute [exp numbers]
  (map (partial power exp) numbers))
查看更多
登录 后发表回答