How to resolve the algorithm Vector step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Vector step by step in the F# programming language

Table of Contents

Problem Statement

Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a pretty print function should be implemented.

The Vector may be initialized in any reasonable way.

The four operations to be implemented are:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vector step by step in the F# programming language

Source code in the fsharp programming language

open System

let add (ax, ay) (bx, by) =
    (ax+bx, ay+by)

let sub (ax, ay) (bx, by) =
    (ax-bx, ay-by)

let mul (ax, ay) c =
    (ax*c, ay*c)

let div (ax, ay) c =
    (ax/c, ay/c)

[<EntryPoint>]
let main _ = 
    let a = (5.0, 7.0)
    let b = (2.0, 3.0)

    printfn "%A" (add a b)
    printfn "%A" (sub a b)
    printfn "%A" (mul a 11.0)
    printfn "%A" (div a 2.0)
    0 // return an integer exit code


  

You may also check:How to resolve the algorithm Reverse words in a string step by step in the Gambas programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the 8th programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the Lua programming language