CS 291 University of Puget Sound Spring, 2020
 
Programming Language Paradigms:
Explorations with Functional Problem Solving (supported by Scheme/Haskell)
and Declarative Prblem Solving (supported by Prolog)
 

Haskell Worksheet 4, due Monday, April 3

This worksheet focuses largely on

These topics are covered in some detail in online readings on Higher order functions, Performance/Laziness, and Lazy Evaluation

Problems for this Worksheet 4 with Haskell

This worksheet expands experience with functional problem solving with Haskell. For this worksheet,

Functional Application and Composition

  1. Consider the following three Haskell functions:

          f x = x*x - 6*x + 9   -- note this is (x - 3)*(x - 3)
          cube x = x*x*x
        
    1. Assuming that f and cube have been defined previously, write a 1-line Haskell function h that computes the composite function cube (f (sqrt x)). The resulting expression for h should not use parentheses!
    2. Test your function on several cases, for which you can easily verify the results (e.g., with a simple hand computation).
    3. Explain briefly, in your own words, why the definition h x = cube f sqrt x does not work.
  2. Transforming Items on a List and Counting How Many Meet a Stated Criterion: A common task is to consider how many items on a list satisfy a given criterion. However, in practice, this work may be divided into three steps:

    Two examples follow:

    Given this framework, a natural approach is to filter the list, based on the transformation and predicate, and then count the number of items present:

          countSpecialItems tr pred lst = length  filter pred tr lst
        

    However, this line does not compile, since Haskell seems confused by the number of functions involved.

    1. Can this line be corrected, using parentheses only? If so, how? If not, explain the trouble(s).
    2. Can this line be corrected, using $ symbols only? If so, how? If not, explain the trouble(s).
    3. Can this line be corrected, using both some parentheses and some $ symbols? If so, how? If not, explain the trouble(s).

Tuples

Note: To access elements within a tuple, pattern matching can work quite well! For example, the following functions provide access to the components of a 4-tuple:

  comp1 (a, _, _, _) = a
  comp2 (_, b, _, _) = b
  comp3 (_, _, c, _) = c
  comp4 (_, _, _, d) = d
  1. Points and Circles: In mathematics, points in the plane are typically identified as (x, y)— that is as a tuple or pair.

    Write a Haskell function inCircle that has three parameters:

    The function should return true if the second tuple pair lies within a circle of the given radius, centered at the first tuple pair. The function should return false otherwise.

    Programming Notes:

  2. Searching a Symbol Table: Years ago, when reading a program, compilers would keep information about symbols (variables, function names, other identifiers) in a structure, historically called a Symbol Table. (More modern usage calls this structure a Keyed Table, as this type of storage and retrieval of data applies much more generally than recording symbols and their properties within a compiler.)

    For this problem, suppose three properties are stored for each variable:

    For this problem, suppose the information for a symbol is stored within a triple or 3-tuple, and the collection of each symbol information is stored on a list.

    Write a function that has two parameters:

    If information for the symbol name is stored in the list, the function should return a pair or 2-tuple containing the type and location for that variable. If the variable name is not found, the function should return the pair ("undefined", 0).

  3. A Simple Course Directory: An abbreviated version of this semester's computer science offerings is shown in the following table:

    Course
    Number
    Section Title Instructor Units
    161 A Computer Science I Xi Chen 1.0
    161 B Computer Science I David Chiu 1.0
    240 Software Engineering Xi Chen 1.0
    261 A Computer Science II Henry Walker 1.0
    261 B Computer Science II Brad Richards 1.0
    291 Programming Language Paradigms Henry Walker 1.0
    315 Computer Graphics Xi Chen 1.0
    395 Computing: Opportunities and Risks Henry Walker 0.5
    440 Capstone in Computer Science David Chiu 1.0
    475 Operating Systems David Chiu 1.0
    481 Compilers and Compiler Writing Brad Richards 1.0
    1. Translate this table into a list of courses, where each course is represented by a Haskell tuple with five components.
    2. Write a Haskell function that has two parameters:
      • a course number (an integer)
      • the list of courses from part a.
      and returns a list of all course tuples with the given course number.

Merge Sort and Lazy Evaluation

As background for this section, refer to the following readings from CSCI 261A:

In addition, the CSCI 291 reading on Performance/Laziness discusses several approaches for the Merge Sort for lists in Haskell.

In summary, a merge sort involves the following recursive outline:

In reviewing this recursive algorithm, two basic functions are needed:

The following questions consider alternative implementations of the Cleave and Merge functions.

  1. The Cleave Function and Efficiency: The reading on Performance/Laziness includes the following two approaches for dividing a list into two pieces:

    In this section of the worksheet, two additional approaches also are considered:

    1. Run and record the output for test cases with each of these functions to determine if they properly. If the code does not work properly, make an appropriate correction.
    2. Suppose that each of these functions are applied to a list of size n. Determine the efficiency (Big-O) of each algorithm, and provide sufficient analysis to justify your answer.
  2. The Merge Function, Lazy Evaluation, and Efficiency The reading on Performance/Laziness includes one approaches for merging two ordered lists into a sorted result. A common variation also is widely reported:

    1. Given two lists of length n, both of these algorithms run in O(n). Briefly explain why.
    2. Although both algorithms have O(n), can anything be said about their relative efficiency? Explain briefly.
  3. Sorting and infinite lists:

    1. Which, if any, of the cleave versions could be used with lazy evaluation of potentially infinite lists? Briefly justify your answer.
    2. Repeat part a for the versions of merge.
  4. Stable Sorting: Following common computing terminology, a sorting algorithm is said to be stable, if when A and B are elements have the same key and A is in a collection before B prior to sorting, then A is still before B in the sorted collection. See WIkipedia's Category:Stable sorts for more details.

    Consider the four versions of Cleave and the two versions of Merge. Which combinations, if any, of these versions yield s stable sort? Explain briefly (both why any combinations are stable and why any other combinations are not).

  5. Two Haskell functions are proposed for reversing the elements on a list.

    In analyzing this code, three observations seem relevant:

    Determine the efficiency (Big-O) of both Approach 1 and Approach 2 for reversing a list of n elements, and provide sufficient analysis to justify your answer.

Extra Credit Problem

  1. Alternative Sorting of a Directory: Expand the list of course tuples from Problem 5, so that it has multiple sections of courses taught by the same instructor. For example, there might be another section (section C) of 161 taught by Xi Chen and two more sections of 261 (sections C and D) taught taught by Henry Walker.

    With this expansion of the course list, modify the Merge Sort, so that it sorts the list by course title. If two course sections have the same title, ordering should be determined by the instructor's name. Finally, if two courses have the same title and the same instruction, ordering should be determined by the section letter.

    Although testing should work with your expanded course tuples, the function itself should work for any directory containing courses using this tuple structure.


created 14 March 2020
revised 25-26 March 2020
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.