We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Omitting type definitions and formalities (which are very important in the real world) I'd start from the building blocks and write an action (not a function) that reads a pair of values from a line and returns a tuple, something like this:
Finally I'd write an action that parses the first line (number of testcases) and calls readTestCase appropriately, aggregating all testcases in a list:
Hope you get the idea. This is not the most elegant way and it looks fairly procedural but many IO actions looks procedural by nature.
If this was a real world piece of code, I'd work on defing my own data type (e.g. a Mapping could be an (Int, Int) tupe, a PossibleFunction could be a list of mappings [Mapping], etc.)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Functions or Not?
You are viewing a single comment's thread. Return to all comments →
Omitting type definitions and formalities (which are very important in the real world) I'd start from the building blocks and write an action (not a function) that reads a pair of values from a line and returns a tuple, something like this:
readPair is clunky and can be written much more elegantly but you get the gist
then I'd write an action that reads a test case, which is made by an integer (the number of pairs) and a list of pairs, something like this:
Finally I'd write an action that parses the first line (number of testcases) and calls readTestCase appropriately, aggregating all testcases in a list:
Finally you can use this IO action in your main:
Hope you get the idea. This is not the most elegant way and it looks fairly procedural but many IO actions looks procedural by nature.
If this was a real world piece of code, I'd work on defing my own data type (e.g. a
Mapping
could be an(Int, Int)
tupe, aPossibleFunction
could be a list of mappings[Mapping]
, etc.)