Question: 1. In Haskell, the pattern [] represents the empty list, and the pattern h:t represents a list with head h and tail t. Consider
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.
Step by Step Solution
3.40 Rating (147 Votes )
There are 3 Steps involved in it
It seems youve shared an image containing a computer science question about Haskell and Prolog ... View full answer
Get step-by-step solutions from verified subject matter experts
