CS 3304 Homework #6

Date Assigned: October 17, 2003
Date Due: October 24, 2003, in class, before class starts
  1. (10 points) Write a ML function called makeset which takes a list of integers as input and removes the repeating elements (the answer is returned as a list). Thus, makeset([1,3,4,1,3,9]) returns [1,3,4,9]. The order in which the elements are returned in the answer doesn't matter, as long as there are no repeats.

  2. (40 points) In this problem you are going to define an ML datatype for boolean expressions. Boolean expressions are made up of boolean values, boolean variables, and operators. There are two boolean values, i.e., true or false. A boolean variable (e.g., "p") is one which can taken on any of the two boolean values. Boolean expressions are constructed from boolean variables and values using the operators AND, OR, and NOT (with their obvious meanings). An example of a boolean expression is AND(OR(p,q), NOT(q)), where p and q are boolean variables. Another is AND(p, true). Do the following:

    • Devise an ML datatype (see lecture summaries from Oct 10) called boolexp whose values represent legal boolean expressions. You may assume that boolean variables (but not the expressions themselves) are represented as strings.

    • Write a function eval(E,L) that takes a boolean expression E and a list of true boolean variables L, and determines the truth value of E on the assumption that the boolean variables in L are true and all other boolean variables are false.

      For instance, if E represents AND(OR(p,q), NOT(q)) and L is the list [p], then the result of eval(E,L) must be true. Please keep in mind that E is not a string but a value constructed from the boolexp datatype.

    You may assume the following ML list member function is available to you (and need not include its definition in your answer).
    fun member (x, nil) = false
    |   member (x, y::ys) = x = y orelse member (x, ys);
    
    This problem can be completed with at most one datatype definition and one short (5 line) ML eval function. Anything more elaborate will be given zero points.

Return Home