Question: A function to count the number of elements in a list of integers that are within some range (inclusively) is straightforward in either Haskell or
A function to count the number of elements in a list of integers that are within some range (inclusively) is straightforward in either Haskell or Prolog. In Haskell, it would have three parameters (the list of integers and the lower and upper bound of the) and would return a single value (the number of elements within that range). In Prolog, it would be a predicate of arity four that succeeds when the fourth element is the number of elements in the first list argument that are in the range specified by the second and third arguments (and you could leave the fourth element uninstantiated to have Prolog count it for you).
HASKELL > countInRange [2,4,6,8,10,12,14,16,18] 3 11 4
PROLOG ?- countInRange([2,4,6,8,10,12,14,16,18], 3, 11, X). X=4; false.
For this question, you will write this function twice - once using Haskell and once using Prolog. Both solutions must be recursive, the Haskell solution must be tail-call optimized, and neither solution may use higher order functions.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
