Question
To answer this question i need the Haskell Code and the Answers to 1-8 (basically the output). Click the run button on the left to
To answer this question i need the Haskell Code and the Answers to 1-8 (basically the output).
Click the "run" button on the left to load the given definitions (in the code part on the left).
1. The Haskell interpreter, GHCi, can evaluate mathematical expressions. In your GHCi terminal (in the lower left of the screen), type the following and then hit enter: What is the answer that is printed to the screen? Write your answer in the answer section (in the panel on the top left).
2. Write a list range or a list comprehension that generates the list of all even positive integers up to and including 100 (hint: if you want to use a list comprehension, note that the function even will return True if the argument passed to it is an even number).Write your answer in the answer section (in the panel on the top left).
3. Write a list comprehension that generates the list of all positive integers up to and including 100 that are divisible by 3 (hint: use the mod function). Write your answer in the answer section (in the panel on the top left).
4. Write a list comprehension that generates the list of all ordered pairs (x,y) where x is in the inclusive range 1 to 20, y is in the inclusive range 1 to 20, and (x + y > 8). Write your answer in the answer section (in the panel on the top left).
5. The following is an example of a function in Haskell:
f :: Integer -> Integer f x = x + 3
This function, f, is a function that maps an integer to another integer. It has one parameter, x. When you call this function, you can pass it an integer as an argument, and it will add 3 to that integer and return the sum. An example of a statement that calls our function f is:
f 7
Enter the statement above that defines the function f into the code window (on the upper left of your screen). After you enter the statement, click the "run" button to load your definition. Then in the GHCi terminal, test out your function (trying calling it with a statement such as f 23) Define a function, g, that takes one integer argument, subtracts 2 from the argument, multiplies the difference by 7, and returns that product. Write your answer in the answer section (in the panel on the top left).
6. Define a function, h, that maps an integer to another integer. This function should return 1 if the argument passed to it is greater than 10; otherwise, it should return 0. Write your answer in the answer section (in the panel on the top left).
7. A function can have more than one parameter. To define a function that has two integer parameters and returns an integer, use syntax like the following:
calcPerimeter :: Integer -> Integer -> Integer
Complete the code for the calcPerimeter function so that this function returns the perimeter of a rectangle. Write your answer in the answer section (in the panel on the top left).
8. Recall that, given two functions f(x) and g(x), we can define the composition function as follows:
(f o g)(x) = f(g(x))
In functional programming languages like Haskell, combining simple functions to create more complex functions is very common and powerful. Haskell has a simple syntax for composing functions: assuming you have two function f and g defined, you can create the composition of f and g using the "dot" operator:
(f . g)
You can also create the composition of g and f like this:
(g . f)
We have already defined 2 functions, f and g. Let's combine them using function composition.
Make sure that your function definitions are loaded. Then, in the GHCi terminal, enter the following function call:
(f . g) 6
What is the result? Then enter:
(g . f) 9
Again, what is the result?
Write your 2 results in the answer section (in the panel on the top left).
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