The reader may have noticed in the previous examples that Haskell infers the types of values (e.g., lists, tuples, and functions) that have not been explicitly declared by the programmer to be of a particular type with the :: operator.
The following transcript demonstrates type inference.
The following transcript demonstrates the use of explicitly declared types.
Exercise C.1 Define a recursive Haskell function remove that accepts only a list and an integer i as arguments and returns another list that is the same as the input list, but with the ith element of the input list removed. If the length of the input list is less than i, return the same list. Assume that i = 1 refers to the first element of the list.
Examples:
Exercise C.2 Define a Haskell function called makeset that accepts only a list of integers as input and returns the list with any repeating elements removed. The order in which the elements appear in the returned list does not matter, as long as there are no duplicate elements. Do not use any user-defined auxiliary functions, except elem.
Examples:
Exercise C.3 Define a Haskell function cycle1 that accepts only a list and an integer i as arguments and cycles the list i times. Do not use any user-defined auxiliary functions.
Examples:
Exercise C.4 Define a Haskell function transpose that accepts a list as its only argument and returns that list with adjacent elements transposed. Specifically, transpose accepts an input list of the form [e1, e2, e3, e4, e5, e6 … , en] and returns a list of the form [e2, e1, e4, e3, e6, e5, … , en, en–1] as output. If n is odd, en will continue to be the last element of the list. Do not use any user-defined auxiliary functions and do not use ++ (i.e., append).
Examples:
Exercise C.5 Define a Haskell function oddevensum that accepts only a list of integers as an argument and returns a pair consisting of the sum of the odd and even positions of the list. Do not use any user-defined auxiliary functions.
Examples:
Exercise C.6 Define a Haskell function permutations that accepts only a list representing a set as an argument and returns a list of all permutations of that list as a list of lists. You will need to define some nested auxiliary functions. Try to define only one auxiliary function and pass a λ-function to map within the body of that function and within the body of the permutations function to simplify their definitions. Hint: Use the built-in Haskell function concat.
Examples:
Hint: This solution requires approximately 10 lines of code.
18.216.55.20