Question
I need code for the following in Haskell: Hercules has a job to do. He has to slay the Hydra. The Hyrdra has nine heads.
I need code for the following in Haskell:
Hercules has a job to do. He has to slay the Hydra. The Hyrdra has nine heads. These are not just any heads; they are level-9 heads. If one of them is cut off, eight level-8 heads grow to replace it. If you chop one of these, seven level-7 heads show up. This continues as you would imagine, until you get to a level-1 head. If you chop that one off, nothing else grows to take its place. The question is this: how many head-choppings does Hercules have to perform to kill the Hydra?1 There are closed-form solutions to this, but let's use recursion to solve this. We will use a list to represent the hydras heads. The initial hydra head count will be represented by [9,0,0,0,0,0,0,0,0]. It shows nine heads of level nine, an no heads of the lower levels. Write a function chop that will take a representation of the Hydra, chop of the highest level head it can get, and return the resulting hydra. Note that chop should run in O(n) time. You can always, always, and forever make helper functions.
Sample run: (chop [9,0,0,0,0,0,0,0,0], chop [0,0,2,0,0,0,0,0,0]) yields ([8,8,0,0,0,0,0,0,0], [0,0,1,6,0,0,0,0,0])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started