Question: In Haskell: For this question you will implement functions that deal with the transaction history of a portfolio of stocks. Each transaction is represented by

In Haskell:

For this question you will implement functions that deal with the transaction history of a portfolio of stocks. Each transaction is represented by a tuple. For example: ('B', 100, 1104, "VTI", 1) The elements of this tuple are: The first element is a character that is either 'B' or 'S'. This represents whether the transaction was buying or selling. The second element is an integer representing how many units were bought or sold. The third element is an integer representing the price per unit that we bought or sold at. The fourth element represents the stock that we bought. The final element represents the day that the transaction took place on. So our example transaction says that we bought 100 units of VTI on day 1, and we paid 1104 per unit. So the total amount that we spent was 100 1104 = 110400. A transaction log is a list of transactions. For example:

type Transaction = (Char, Int, Int, String, Int)

test_log :: [Transaction]

test_log = [('B', 100, 1104, "VTI", 1),

('B', 200, 36, "ONEQ", 3),

('B', 50, 1223, "VTI", 5),

('S', 150, 1240, "VTI", 9),

('B', 100, 229, "IWRD", 10),

('S', 200, 32, "ONEQ", 11),

('S', 100, 210, "IWRD", 12)

]

The goal is to build a function to tell the user how much profit or loss was made on each stock. For our test data set, this report will be VTI: 14450 ONEQ: -800 IWRD: -1900 which indicates that we made 14450 pounds from our trades on VTI, we lost 800 pounds from our trades on ONEQ, and we lost 1900 pounds from our trades on IWRD. Remember that the amount of money we spend when we buy is units price, and the amount of money we gain when we sell is also units price. So to work out the profit or loss from a particular stock, we add up the amount of money we gained by selling, and then we subtract the money that we spent when we bought. The calculation for VTI is (100 1104) (50 1223) + (150 1240) = 14450

Write a function profit_report :: [String] -> [Transaction] -> String that takes a list of stocks and a transaction log, and returns the human-readable string containing the profit and loss report. Specifically, for each stock in the input list, the report should include the line STOCK: PROFIT where STOCK is the name of the stock, and PROFIT is the amount of profit made. The stocks should appear in the order in which they are listed in the input. For example:

ghci> profit_report ["VTI", "ONEQ"] test_log "VTI: 14450 ONEQ: -800 " ghci> profit_report ["VTI"] test_log "VTI: 14450 "

You can test your function using putStr like so: ghci> putStr (profit_report ["VTI", "ONEQ", "IWRD"] test_log) VTI: 14450 ONEQ: -800 IWRD: -1900

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!