Index: Simple data definitions, Donkey, Scheme style
In class we saw the data definition for a list of numbers:
A list of numbers is
It is good practice always to write your data definitions. This will be expected to do this on most Comp210 homeworks. In Scheme, the data definitions are not part of your program, but it is good practice to include them as comments in your code, e.g.,
; A LoN (list of numbers) is one of ; 1) null ; 2) (cons f r), where f is a number, r is a LoNIn some programming languages, some form of data definitions are part of the program and used by the computer. Regardless of programming language, it will be important to clearly remember your data definition when writing your program.
Remember that the program template mimics the data definition. Also, the program template is not a completed function -- it is merely a skeleton of what most functions using this data will look like.
You should provide the template in your homeworks. Since a template isn't valid Scheme (it includes "holes" represented by ellipses), you need to put it in comments.
Our five steps are
Try some other examples:
Hint: as one option, you can either think of an empty list, or building a list with a symbol, or building a list with a number; as another option, you can think of an empty list, or building a list with a symbol or a number. Do you see the difference? There's nothing too mysterious -- they are equivalent, of course.
How would you define even (0, 2, 4, ...) length lists of numbers?
Like DrScheme, Donkey is a programming environment for Scheme. While DrScheme is generally better, Donkey has one important feature that DrScheme lacks: it allows you to see how your program is evaluated. This lab shows you how to use that feature.
To run Donkey, either
As an example, type (if (= 1 2) (+ 1 2) (+ 1 3)) in the main window.
When stepping, you also have other options, e.g., selecting the Unstep button goes backwards one step in the evaluation.
One main option you'll want in Donkey are to Load files (in the File menu). We assume you'll usually use DrScheme for writing your programs. Select Help from the Help menu to explain other options.
Try stepping through some examples. In particular, try a simple use of recursion:
(define length (lambda (l) (cond ((null? l) 0) (else (+ 1 (length (cdr l))))))) (length (cons 1 (cons 4 (cons 8 (cons 2)))))
Since stepping through larger programs can be very tedious, you can indicate what kinds of subexpressions are "important" to show during stepping. In the Stop-at menu, currently All is selected, i.e., stepping stops at all subexpressions. If you select "if/cond/case", stepping will only stop and conditionals, but not at function applications. When stepping through recursive functions, you sometimes want to select only "apply" in the Stop-at menu, i.e., only indicate when it's applying a user-defined function and skip over the conditionals and primitive functions. Try the same example again and observe the difference.
Like any language, there are conventions for writing Scheme that help make it more readable. In this and the previous lab, we've discussed some conventions on providing supplementary information in comments. In class, we briefly discussed conventions for choosing between cond and if. Here, we will discuss layout and indenting conventions. Also of note, programmers vary widely in naming conventions, e.g., whether to name a list of numbers as l, lon, AListOfNumbers, nums, or something else.
The basic goals of spacing and indentation in Scheme include
(define afunction (lambda (x) body))The define and lambda keywords are each on separate lines to point out that you are
(if (= 3 x) (f 5) (+ 7 y))The "then" and "else" branches are on separate lines and aligned so that you can easily see them. The same idea holds for larger conditionals:
(cond ((null? l) 0) (else (+ 1 (foo (cdr l)))))(Note that I like to also align the result expressions in a cond, assuming the test expressions are reasonably short.)
(cond ((null? l) 0) (else (+ 1 (foo (cdr l)))))or in function calls,
(myfunction (+ 3 (* 5 x) y) (- 3 (f 9 z)))
(define afunction (lambda (l) (cond ((null? l) 0) (else (+ 1 (afunction (cdr l)))))))It also makes you scroll vertically more. Unfortunately, sometimes you are forced into this if you want to be able to print your code on fixed-width paper, and you have really big complicated functions.