5

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.

3
  • 10
    Those are neither tuples nor arrays, they are in fact the dreaded hybrid: toupees. Commented Sep 14, 2011 at 19:08
  • 2
    (To be clear, what @Daniel means is that semicolons separate values in array/list literals, whereas commas create tuples.) Commented Sep 14, 2011 at 19:41
  • 1
    Or to put it in another way: the A and B you created are arrays each containing a single element; that single element is a tuple of 4 numbers. Commented Sep 15, 2011 at 6:17

1 Answer 1

23
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.