
Concept explainers
HASKELL
Please submit a single Haskell file
• Please put comments in your code to show which question you are answering with each piece of
code.
• You may create auxiliary functions if you like. You may use library functions from Haskell’s
standard library.
• Please limit your line lengths to 100 characters max.
Please use the following two data types which you can copy-and-paste into your code.
data Action = Rock | Paper | Scissors deriving (Eq, Show)
data Outcome = Player1Win | Player2Win | Draw deriving Show
Action represents a player’s chosen action and Outcome represents the outcome of playing a game.
TASK
Define a function check :: Action -> Action -> Outcome which calculates the outcome of a
game where the first argument is the action of player 1 and the second argument is the action of
player 2.
For example, check Rock Paper = Player2Win and check Paper Paper = Draw.
Define a function parse :: String -> Maybe Action which will be used to parse user input for
the choice of an action. Return Nothing if the input string is not valid, otherwise parse strings
into actions based solely on the first letter, e.g., parse "P" = Just Paper.
Recall the I/O functions in Haskell for inputting a string from the user and outputting a string to the console:
getLine :: IO String
putStrLn :: String -> IO ()
Define a recursive function play :: Integer -> Integer -> IO () which takes two inputs:
the number of wins so far for player 1 and the number of wins so far for player 2. The game should
proceed as follows:
• Print a message asking player 1 to make an action;
• Read input from the user and parse it;
• If the input is invalid according to the parser, print out the outcome of the game, i.e., who had
the most wins and is therefore the overall winner, or whether overall it is a draw, and then
return.
• If the input is valid then:
– Print a message asking player 2 to make an action;
– Read input from the user and parse it;
– If the input is invalid then print out the outcome of the whole game (see above).
– If the input is valid then check to find out who is the winner and print an appropriate
message, then recurse with the updated tally of player 1 and player 2 wins.
Define main to start the game with a score of 0 for both players.
Here is an example interaction which you should try to reproduce:
*Main> main
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
R
Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
P
Player 2 wins!
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
S
Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
R
Player 2 wins!
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
e
Player 2 wins, with 2 games to 0
*Main>
Multiple players can play together in a tournament, playing in pairs with the winner staying on to
play in the next round. In this tournament, a player picks an action at the start and keeps playing
that action, i.e., if they win with action A then in the next game they will again play action A.
Tournaments can be represented by a binary tree, for which we use the following definition:
type PlayerId = Integer
data TournamentTree = Player Action PlayerId | Game TournamentTree TournamentTree
deriving (Eq, Show)
The nodes of the binary tree represent a game that will be played at some point. The leaves of the
tree are players, which have a player id (an integer) and an action that they will take throughout
the tournament. Here is an example tournament setup:
example = Game (Game (Player Rock 1) (Player Scissors 2))
(Game (Player Paper 3) (Player Rock 4))
(a) Define a function playRound :: TournamentTree -> TournamentTree that plays one round
of a tournament, i.e., it traverses the tournament tree and for any game that is ready to be
played, of the form (Game (Player a1 id1) (Player a2 id2)), then the game is played
(using check) and the resulting tournament tree has this node replaced with the winning
player.
For example: playRound example = Game (Player Rock 1) (Player Paper 3)
If there is a draw in a game then its node is left as-is in the output tree.

Step by stepSolved in 3 steps with 1 images

- Which of the following statements is NOT correct? Group of answer choices C++ language provides a set of C++ class templates to implement common data structures, which is known as Standard Template Library (STL); In STL, a map is used to store a collection of entries that consists of keys and their values. Keys must be unique, and values need not be unique; In STL, a set is used to store a collection of elements and it does not allow duplicates; Both set and map class templates in STL are implemented with arrays.arrow_forwarduse Advanced C++ techniques, containers and features to refactor the C/C++ algorithm. The purpose is toreplace appropriate declarations and code segments with Advanced C++ declarations and code. * Replace all arrays with appropriate STL containers. * Use STL algorithms to REPLACE existing logic where appropriate. * Use smart and move pointers where pointers are needed. * Use lambda expressions where appropriate. * Use C++ style casting when needed. * Look for opportunities where tuples could be used. * The code listing is in C and C++. If you don't understand someof the C code or C functions, please google. If there are logic or syntax problems, please fix. int Algorithm(int a[], int x, int y) { int p, i, j = x; p = y; for(int i=x; i < y; i++) { if(a[i] < a[p]) { swap(&a[i], &a[j]); j++; } } swap(&a[p], &a[j]); return j;}arrow_forward1] Write a simple C/C++ program that shows that C/C++ does not check the index range for static one-dimensional arrays, and briefly explain how the program is supposed to check it. 2] Write a simple program in Python to detect whether the scoping of variables is static or dynamic, and briefly explain how the program is supposed to check it.arrow_forward
- need help in C++ Problem: You are asked to create a program for storing the catalog of movies at a DVD store using functions, files, and user-defined structures. The program should let the user read the movie through the file, add, remove, and output movies to the file. For this assignment, you must store the information about the movies in the catalog using a single vector. The vector's data type is a user-defined structure that you must define on functions.h following these rules: Identifier for the user-define structure: movie. Member variables of the structure "movie": name (string), year (int), and genre (string). Note: you must use the identifiers presented before when defining the user-defined structure. Your solution will NOT pass the unit test cases if you do not follow the instructions presented above. The main function is provided (you need to modify the code of the main function to call the user-defined functions described below). The following user-defined functions are…arrow_forwarduse Advanced C++ techniques, containers and features to refactor the C/C++ algorithm. The purpose is toreplace appropriate declarations and code segments with Advanced C++ declarations and code. * Replace all arrays with appropriate STL containers. * Use STL algorithms to REPLACE existing logic where appropriate. * Use smart and move pointers where pointers are needed. * Use lambda expressions where appropriate. * Use C++ style casting when needed. * Look for opportunities where tuples could be used. * The code listing is in C and C++. If there are logic or syntax problems, please fix. #define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L)) char dih[50],dah[50],medium[30],word[30],*dd[2] = {dih,dah};const char *ascii = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?'!/()&:;=+-_\"$@", *itu[] =…arrow_forwardIt’s due in C++ Please tell me which code is for StudentMaim.c and which for studentInfo.h and studentInfo.carrow_forward
- use Advanced C++ techniques, containers and features to refactor the C/C++ algorithm. The purpose is toreplace appropriate declarations and code segments with Advanced C++ declarations and code. * Replace all arrays with appropriate STL containers. * Use STL algorithms to REPLACE existing logic where appropriate. * Use smart and move pointers where pointers are needed. * Use lambda expressions where appropriate. * Use C++ style casting when needed. * Look for opportunities where tuples could be used. * The code listing is in C and C++. If there are logic or syntax problems, please fix. char algorithm1(char c,int s){ if (isalpha(c)) { c = toupper(c); c = (((c - 65) + s) % 26) + 65; } return c;} int algorithm2(string input){ do { string output = ""; int shift = rand() % 26; for (int x = 0; x < input.length(); x++) output += algorithm1(input[x],shift); cout << output << endl; } while…arrow_forwardGive an example of a function with two arguments and statements that are executed when the function is called. -Provide details about the return statement and the purpose of your function. -Provide an example of an array and a vector. -Describe your experience using GitHub, GitLab, Git, or other versional control software in general. Explain what aspects of the GitHub process you are comfortable with and what areas you need more practice or help.arrow_forwardPp# 1: can you help me solve and understand this practice problem please? A step by step explanation would be appreciated. Thank you!arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





