0

I am trying to write the function trade_report :: String -> [Transaction] -> String which will take an element in a list and return the values associated with the element.

For context in what the Type transaction is:

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)
            ]
5
  • 1
    You need to apply transaction_to_string to each element in the result of get_trades, not the list as a whole. (Hint: use map.) Then you want to join the list of strings you get as a result into a single string using "\n" as a separator. (Hint: use Data.List.intercalate, which a normal language would call join :) ) Commented Nov 16, 2022 at 15:26
  • To add to @chepner's comment you'll also have to filter out the transactions that don't satisfy the criteria. you can use the filter function for that. The data flow would look some thing like this: transaction_log -> filter logs that you need -> convert each transaction to string -> intercalate the strings. Commented Nov 16, 2022 at 17:10
  • @Apoorv I think that's what get_trades is doing. Commented Nov 16, 2022 at 17:14
  • I have used the map and filter already. Please refer to the edited code. Apologies for not adding it in initially Commented Nov 16, 2022 at 17:14
  • At present, this isn't a question. It just says "I am trying to write this function trade_report" (without even really specifying what it does) and gives some auxiliary definitions for context. To help you we need to know what code you have written to try to solve this, and what specifically (full error messages, etc) isn't working. I can see in the edit history you have removed a lot of material; should some of that come back? Commented Nov 22, 2022 at 23:26

1 Answer 1

1

get_trades returns a list of Transaction values. transaction_to_string only takes a single Transaction value. You need to use map.

> :t transaction_to_string
transaction_to_string :: Transaction -> String
> :t map transaction_to_string
map transaction_to_string :: [Transaction] -> [String]

Once you have your list of String values, you can join them together with \n characters using Data.List.intercalate.

> import Data.List
> intercalate "\n" ["foo", "bar"]
"foo\nbar"
Sign up to request clarification or add additional context in comments.

1 Comment

How would you do this without importing Data.List? How could I write it as a single function for trade_report

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.