Question A few questions about ML/the assigment (long) 

Forum: Com Sci 221, U Chicago, Autumn 2001, Discuss Classwork
Re: News Assignment #8 due Monday, 26 November (Mike O'Donnell)
Date: 2001, Nov 19
From: Ben Wolfson wolfson

The first one of these has nothing to do with the assignment, but it's frustrating me anyway.
  • I noticed that I can get three-way comparisons of certain types using a compare function, eg Char.compare or Int.compare. But there is no general three-way compare. I was writing a function to see if one list is "less" than another using three-way compare, and since I knew I would only be able use it on char lists I could do this:
    fun listlt ([]:char list) [] = false
     |  listlt [] ys = true
     |  listlt xs [] = false
     |  listlt (x::xs) (y::ys) (case (Char.compare (x,y))
                                    of LESS => true
                                     | EQUAL => listlt xs ys
                                     | GREATER => false);
    
    But if you don't know you'll only be working with lists of a given type, that approach won't work. So I tried writing a compare using < and =, like this:
    fun compare x y = if x < y then LESS else if x = y then EQUAL else GREATER;
    
    and that worked, but only for ints. So I tried explicitly making the function polymorphic, which earned me a fun error message:
    - fun compare (x:'a) (y:'a) = if x < y then LESS else if x = y then EQUAL else GREATER;
    stdIn:2.24-4.3 Error: operator and operand don't agree [UBOUND match]
      operator domain: 'Z * 'Z
      operand:         'a * 'a
      in expression:
        x < y
    stdIn:3.8-4.3 Error: operator and operand don't agree [UBOUND match]
      operator domain: ''Z * ''Z
      operand:         'a * 'a
      in expression:
        x = y
    - 
    
    I suppose the compiler could be objecting because, when it compiles the function, it has no way of knowing if it will be called with types for which < and = are defined, but I doubt that's it. It seems kind of pointless to have to define (as is done with lt and ltchar in the assignment statement) multiple functions that do the same thing; in this respect it seems no better than C. Is there actually no way to write functions like a generic three-way compare?
  • Is the lexcomp function defined locally in anadict supposed to be different from the globally defined lexcomp? My inclination is to assume that they should be different, because otherwise there'd be no point in having two definitions, but I can't imagine how two functions with that name could differ.
  • In the skeleton definition of anasort, there are ellipses in front of, between and behind the two (explode ...) calls. Does that mean that a solution that only inserts things in the first two positions will be wrong? Or is the final ellipsis just for closing parentheses?
  • The problem statement for the second problem mentions pairs in the second to last sentence, and the anadict skeleton has a local makeanapair function, but the rest of the problem statement only mentions a list with sublists. Are the pairs hd/tl pairs, or is the result supposed to be (string * string list) list, with the first string in each list the anagram-canonical form?

I think that's everything.


Messages

1. Feedback 3-way compare depends on 2-way, ... by Mike O'Donnell, 2001, Nov 20

to: "A few questions about ML/the assigment (long)"