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.
The idea is to start traversing both the array simultaneously from the end until we reach the 0th index of either of the array. While traversing each elements of array, add element of both the array and carry from the previous sum. Now store the unit digit of the sum and forward carry for the next index sum.
In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.
To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With