CS 2135: Lab 2


Lab Motivation and Goals

This lab is designed to help you practice

Notes


Exercises: Functions as Arguments

The lab2-teachpack provides a general-purpose sort function. The contract on sort is as follows (where "alpha" stands for any type, such as number, symbol, or dillo):

;; sort : (alpha alpha -> boolean) list-of-alpha -> list-of-alpha

The first argument to sort is a function that compares two Scheme values and indicates when the first is less than the second. The second argument to sort is the list to sort. Note that the inputs to the ordering function (the first arg to sort) must be the same type as the elements of the list to sort (the second arg to sort).

  1. Let L be the list (cons 8 (cons 4 (cons 9 (cons 1 (cons 2 empty))))).

    Using sort, write an expression that sorts L into increasing order. Write another expression that sorts L into decreasing order.

  2. Recall our structures for animals (we will treat the sells field in tigers as symbols for this exercise):

    ;; a boa is a (make-boa symbol number symbol)
    (define-struct boa (name length eats))
    
    ;; a dillo is a (make-dillo number boolean)
    (define-struct dillo (length dead?))
    
    ;; a tiger is a (make-tiger symbol number symbol)
    (define-struct tiger (name length sells))
    

    Using sort, write a function sort-boas-by-length that consumes a list of boas and returns a list of boas. The returned list should contain the boas from the input list, but sorted in order of increasing weight.

  3. Using sort, write a function sort-animals-by-type that consumes a list of animals and returns a list of animals. The returned list should contain the animals from the input list, but with all the boas appearing at the front of the list, followed by the dillos, followed by the tigers. Within each type of animal, the animals can appear in any order.

The teachpack also provides a function for creating simple GUIs for applications that take two numbers as arguments, perform some computation on them, and displays the answer. This function is called make-gui, and has the following contract:

;; make-gui : string string (number number -> value)

The first string gives the title of the GUI window, the second string provides the label on the button, and the third argument is the function that should be run on the numbers input via the GUI when the button is pressed. For example, the call

(make-gui "My Adder" "Add Now" +)

implements a simple application for adding numbers. Evaluate this expression in DrScheme to see the GUI in action.

  1. Use make-gui to implement an application that displays the sum of the squares of the two input numbers.

    Everybody should be able to finish up to this point

  2. Use make-gui to implement a numbers guessing game. The game fixes two numbers that the player has to guess (in order). When the player clicks the button, the game returns one of the answers "you win!", "one right", or "both wrong", depending on whether the inputs match the expected numbers.

Exercise: Writing Sorts with Comparators as Arguments

Finish your insert-sort program from Monday, then modify it to take the comparison operator as an argument. Can you use your program to sort lists of things other than numbers? (we wrote it for numbers in class.) Why or why not? If you think you can use it on other lists, show how to use your sorting routine instead of the one from the teachpack for the earlier lab exercises.

Exercises: Functions as Return Values

In Scheme, functions can return other functions as the result of computations. This requires a keyword called lambda, where you can think of lambda as being like a "make-function" operator. Here's a simple example:

(define (make-multiply-by num)
  (lambda (x) (* num x)))

(define double (make-multiply-by 2))
(define triple (make-multiply-by 3))

>(double 3)
6

The (lambda (x) (* num x)) creates a function with one parameter (called x), and a body of (* num x). This is similar to (define (mymult x) (* num x)); the difference is that the lambda version doesn't associate a name with the function. Type in and experiment with the make-multiply-num code above to get a feel for how lambda works.

  1. [continuing the numbers game from the last problem] Write a function make-compare-guesses that consumes two numbers (the numbers a player is trying to guess) and returns a function. The returned function should take two numbers as arguments (the guesses) and returns one of the strings "you win!", "one right", or "both wrong", depending on whether the inputs match the expected numbers.

  2. Re-implement the numbers game using make-compare-guesses.