Exercises in this lecture  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 2.11
Generating a C-style compare function **


A C-style compare function (cmp x y) returns -1 if x is less than y, 0 if x is equal to y, and 1 if x is greater than y.

Write a higher-order function make-comparator, that takes two functions lt (less than) and gt (greater than) as parameters and generates a C-style compare function.

With a little more thought we can generate a C-style compare function from only the function lt. Give it a try.

Use make-comparator and a standard function string<? to generate a function that compares two strings in a C-style (a Scheme version of strcmp).

The other way around, write a higher-order function that takes a C-style compare function and generates a list of functions: lt, equal, gt. Test each of the three generated functions and make sure they work as expected.


Solution