For this lab, you'll need to use the lab2-teachpack. Download this file to your machine, then select the "Add Teachpack" option under the Language menu. Select the file you just downloaded, then click Execute to load the teachpack into DrScheme (teachpacks are DrScheme's libraries).
As of this lab, move to language level "Intermediate student with lambda" (under the "Language" menu in DrScheme).
Scheme has a built-in operator list
that you can
use to build lists. Instead of writing
(cons 1 (cons 7 (cons 5 empty)))
you can write (list 1 7 5)
. Note that the list
version does not contain the empty
. You can use
cons
or list
to write down lists for your
test cases (but don't use list
in place of
cons
inside your functions).
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).
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.
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.
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.
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
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.
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.
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.
[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.
Re-implement the numbers game using
make-compare-guesses
.