How do I add two numeric arrays in F#

末鹿安然 提交于 2019-12-10 15:42:54

问题


I am totally new to F#. I have searched high and low but I cannot find an example for what I want.

let A = [| 1.0, 2.0, 3.0, 4.0 |];; //maybe delimiter with ;
let B = [| 4.0, 3.5, 2.5, 0.5 |];;

let C = A + B;; //how do I define the addition operator for arrays?
// expect C=[| 5.0, 5.5, 5.5, 4.5 |]

I have come close with this posting, but it is not what I want.


回答1:


let inline (++) a b = Array.map2 (+) a b

let A = [| 1.0; 2.0; 3.0; 4.0 |];;
let B = [| 4.0; 3.5; 2.5; 0.5 |];;
let A1 = [| 1; 2; 3; 1 |];;
let B1 = [| 4; 3; 2; 1 |];;

let C = A ++ B
let C1 = A1 ++ B1


来源:https://stackoverflow.com/questions/7421527/how-do-i-add-two-numeric-arrays-in-f

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