1. In Haskell, the pattern [] represents the empty list, and the pattern h:t represents a...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. In Haskell, the pattern [] represents the empty list, and the pattern h:t represents a list with head h and tail t. Consider the following definition of the functions generate and filter: generate x = x: (generate (x+2)) filter = 0 filter f (h:t) = if (f h) then h:filter f t else filter f t (a) Write TWO expressions in Haskell that will remove all even numbers from a list containing the first 100 numbers (1 to 100). The first should use the function filter defined above, and the second should use list comprehension. You are expected to use a lambda expression in both expressions (only one-off functions should be used). (b) Define a function head, including the type signature, in Haskell that returns the first element (i.e., the head) of a list given as an argument. What is the result of evaluating the following expression in Haskell using your function? Justify your answer. head (generate 1) (c) Give the most general type for the function filter defined above. Show all your working. (d) Write Prolog clauses to filter a list so that it contains only odd numbers (you can assume predicates for even/odd in case these are useful to your answer). Give a trace of your clauses for the following query: filter ([1,2,3], R) which should succeed with R= [1,3]. Your trace should show the substitutions at each step. 1. In Haskell, the pattern [] represents the empty list, and the pattern h:t represents a list with head h and tail t. Consider the following definition of the functions generate and filter: generate x = x: (generate (x+2)) filter = 0 filter f (h:t) = if (f h) then h:filter f t else filter f t (a) Write TWO expressions in Haskell that will remove all even numbers from a list containing the first 100 numbers (1 to 100). The first should use the function filter defined above, and the second should use list comprehension. You are expected to use a lambda expression in both expressions (only one-off functions should be used). (b) Define a function head, including the type signature, in Haskell that returns the first element (i.e., the head) of a list given as an argument. What is the result of evaluating the following expression in Haskell using your function? Justify your answer. head (generate 1) (c) Give the most general type for the function filter defined above. Show all your working. (d) Write Prolog clauses to filter a list so that it contains only odd numbers (you can assume predicates for even/odd in case these are useful to your answer). Give a trace of your clauses for the following query: filter ([1,2,3], R) which should succeed with R= [1,3]. Your trace should show the substitutions at each step.
Expert Answer:
Answer rating: 100% (QA)
It seems youve shared an image containing a computer science question about Haskell and Prolog ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
5.In a nut shell, when you look back over time, the power of image has always been there. Even in the prehistoric era, they used imagery to communicate, and what's even more incredible is that we are...
-
According to Figure 2.4, (a) Did the quantity of manufactured output increase or decrease between 1900 and 2000? (b) By how much (in percentage terms)? Use the percentage change formula = [(new value...
-
In Problems 23 through 32 the eigenvalues of the coefficient matrix A are given. Find a general solution of the indicated system x' = Ax. Especially in Problems 29 through 32, use of a computer...
-
The chance that it will rain tomorrow is 25%. Classify the statement as an example of classical probability, empirical probability, or subjective probability. Explain your reasoning.
-
Stevens Co. bought a machine on January 1, 2006 for $875,000. It had a $75,000 estimated residual value and a ten-year life. An expense account was debited on the purchase date. Stevens uses...
-
Read the following article, A Day in the Life of a PreK Teacher . Share your thoughts about how this way of life each day will suit you well in your strengths as a teacher and where you anticipate...
-
Refer to your answers to Problem 31-7 when answering the following questions. a. Which one of the following rates of exchange of portable power banks for flash memory drives will be acceptable to...
-
Given the following information, determine the cost of goods sold for the period. Date Jan 1 Beginning Inventory $950 Jan 1-30 Purchases $1800 Jan 31 Ending Inventory $250.
-
(b) What is the shortest word in * accepted by P? Show that your chosen word is accepted. (c) Is the language L(P) accepted by P a regular language? If so, give a regular expression. If not, explain...
-
Blake lives in Seattle and is planning a get-away with some friends to San Diego and needs to find a hotel for her group. Blake enters "discount hotels san diego" in the Google search bar. The...
-
Compute the annual dollar changes and percent changes for each of the following items. Note: Decreases should be entered with a minus sign. Round your percentage answers to one decimal place.
-
Give two examples of even functions and two examples of odd functions that are NOT polynomial. Give the equations and graphs (you can sketch them on the same set of axis below. -10 -$ flay-10 -51 -10...
-
Suppose that S = {u, 12, 13, 14} where 72 56 u = 23 199 -144 -113 197 u = -299 -187 60 244 -290 u3 = 72, 23, 199, -144, -113, 197 56, -299, -187, 60, 244, -290 291, 267, -233, -13, -95, 179 -201, 18,...
-
Southern Chemical Sdn Bhd (SC) is pharmaceutical manufacturer, it has the following assets: a) SC has the following items in its office: RM Printer 4,000 5,000 Office desk (10 units) Office chair (10...
-
Extend Algorithms 3.4 and 3.5 to include as output the first and second derivatives of the spline at the nodes.
-
Skyler is covered by his company's health insurance plan. The health insurance costs his company $3,500 a year. During the year, Skyler is diagnosed with a serious illness and the health insurance...
-
During 2012, Tom sold Sears stock for $10,000. The stock was purchased 4 years ago for $13,000. Tom also sold Ford Motor Company bonds for $35,000. The bonds were purchased 2 months ago for $30,000....
-
How much of each of the following prizes or awards is taxable? a. Cheline received a $50,000 gift bag at the Oscars in 2012. b. Jon received a gold watch worth $350 for 25 years of service to his...
-
The chief goal of the private sector is effciency.Why? The primary focus of the public sector is equity. Why? It is quite possible to be highly effcient yet ineffective. It is also possible to be...
-
Why is healthcare reform such a contentious issue in the United States? Why have public and private efforts been unsuccessful in fully addressing the challenge of affordable health care? What role(s)...
-
Drawing on economic theory, what kinds of market failure underlie the worlds most pressing problems? Examine three examples: hunger, groundwater contamination, and literacy.
Study smarter with the SolutionInn App