C.10 Declaring Types

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.

C.10.1 Inferred or Deduced

The following transcript demonstrates type inference.

A set of 26 code lines in Haskell that demonstrates type inference.
Description

C.10.2 Explicitly Declared

The following transcript demonstrates the use of explicitly declared types.

A set of 11 code lines in Haskell that uses explicitly declared types.
Description
Continuation of the code in Haskell that uses explicitly declared types, consisting of 41 lines.
Description

Programming Exercises for Appendix C

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:

A set of four code lines in Haskell with the function remove.
Description
Continuation of the code in Haskell with the function remove, consisting of six lines.
Description

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:

A set of six code lines in Haskell with the function make set.
Description

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:

A set of 14 code lines in Haskell with the function cycle 1.
Description

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:

A set of two code lines in Haskell with the function transpose.
Description
Continuation of the code in Haskell with the function transpose consisting of four lines.
Description

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:

A set of 14 code lines in Haskell with the function odd even sum.
Description

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:

A set of 18 code lines in Haskell with the function permutations.
Description

Hint: This solution requires approximately 10 lines of code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.219