Raising elements in a vector to a power

谁都会走 提交于 2019-12-13 07:27:24

问题


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))

回答1:


You need something like:

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

For more information, type the following in your REPL:

(doc map)



回答2:


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

(defn compute [exp numbers]
  (map (partial power exp) numbers))


来源:https://stackoverflow.com/questions/21189258/raising-elements-in-a-vector-to-a-power

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!