Core Types: Symbols, Lists, and Numbers

Summary: In this lab, you will explore the basic operations for working with three core types in Scheme: lists, symbolic values, and numbers.

Related Pages:

Key Procedures:

• List constructors used in this lab: `cons` and `list`.
• List extractors used in this lab: `car`, `cdr`, `length`, and `list-ref`.
• Other list procedures used in this lab: `append` and `reverse`.
• Number predicates used in this lab: `complex?`, `exact?`, `inexact?`, `number?`, `rational?`, and `real?`,
• Other number procedures used in this lab: `denominator`,and `numerator`.

Contents:

Exercises

Start DrScheme.

Exercise 1: Two simple lists

a. Call the `cons` procedure to create a list that has the number 1 as its first and only element. The result of your call should be `(1)`.

b. Call the `cons` procedure to create a list that has the symbols `a` and `b` as its two elements. The result of your call should be `(a b)`.

Exercise 2: Interpreting compound `cons`es

a. Figure out (without using DrScheme) the result of the following expression.

```(cons 'alpha (cons 'beta (cons 'gamma (cons 'delta null))))
```

Exercise 3: Using the `list` procedure

Call the procedure `list`, supplying the numerals `17` and `43` as arguments. Describe the value returned by the procedure.

Exercise 4: Empty lists

How would you invoke the `list` procedure to create an empty list?

Exercise 5: Repeated elements

Determine by experiment whether it is possible to create a list in which the same element occurs more than once.

Exercise 6: Experiments with `car` and `cdr`

a. What is the `cdr` of a one-element list?

c. It makes no sense to apply the `car` and `cdr` procedures to an empty list, because there's no way to split off the first element of a list that has no elements. What happens if you try it anyway? Find out by having DrScheme evaluate a deliberately incorrect procedure call.

d. Does it make sense to apply `car` and `cdr` to values other than lists? Why or why not?

e. Determine what happens if you apply these procedures to symbolic values and numeric values.

Exercise 7: Compound lists

a. Create the list `(e)`.

b. Create the list `(d (e))`.

c. Create the list `(b c)`.

d. Create the list `(a (b c) (d (e)))` using `list`.

e. Create the list `(a (b c) (d (e)))` using `cons`.

Exercise 8: How long is emptiness?

Determine the length of the empty list.

Exercise 9: Length of compound lists

a. Create the list `(a (b c) (d (e)))`

b. What do you think the length of this list should be?

c. Experimentally determine the length of this list.

d. Explain the result.

Exercise 10: Reversing lists

Use Scheme to compute the reversal of the list whose elements are the symbols `senior`, `junior`, `sophomore`, and `freshling`, in that order.

Exercise 11: Reversing compound lists

a. If a list has another list as one of its elements, should `reverse` reverse that inner list as well as the outer one? Argue your case.

b. Find out by experiment what Scheme does.

Exercise 12: Three Methods of Joining lists

a. Use Scheme to find the result of stringing together (with `append`) a list with the symbols `alpha` and `beta` as its elements and a list with the numbers 1, 2, and 3 as its elements.

b. How many elements does the resulting list have?

c. Invoke the procedure `list`, applying it to the two lists that you strung together in the previous exercise: a list with the symbols `alpha` and `beta` as its elements and a list with the numbers 1, 2, and 3 as its elements.

d. How many elements does the resulting list have?

e. The answer to this question is different from the answer to b. Why?

f. Write a call to the procedure `cons`, applying it to our favorite two lists: a list with the symbols `alpha` and `beta` as its elements and a list with the numbers 1, 2, and 3 as its elements.

g. How many elements does the resulting list have?

h. Why is the answer to this question different from the answers to b and d?

If you are puzzled by this exercise, you may want to read the notes on the exercise.

Exercise 13: Extracting elements

Write a call to the `list-ref` procedure that will extract the fourth element of the list

```(38 72 apple -1/3 sample)
```

That is, you should extract the number -1/3.

Exercise 14: Types of Numbers

Have DrScheme confirm that 3/4 is a rational number but not an integer and that the square root of -1 is a complex number but not a real number.

Exercise 15: The Type of a Square Root

Confirm that the value DrScheme computes for `(sqrt 2)` is an inexact real that is also rational.

Exercise 16: More Types of Numbers

As you've just seen, some kinds of numbers are subsets of other kinds of numbers. Determine the relationships between the various kinds of numbers.

Exercise 17: Inexact Roots

a. Have DrScheme find the square of the square root of 2 and subtract 2 from the result.

b. Ideally, the difference should be 0; why isn't it?

c. How big is the difference?

d. Will you have the same problem if you start with 4? Why or why not?

Exercise 18: Other Integer Procedures

Scheme provides a number of numerical procedures that can produce integer results.

Here are some others. For each, try to figure (by experimentation, by discussing results with other students, and, eventually, by reading documentation) out how many parameters each procedure can take and what the procedure does. You may not be able to figure all of them out. Make sure to try a variety of values for each procedure, including positive and negative, integer and real.

a. `quotient`

b. `remainder`

c. `modulo`

d. `max`

e. `min`

f. `numerator`

g. `denominator`

h. `gcd`

i. `lcm`

j. `floor`

k. `ceiling`

l. `truncate`

m. `round`

Exercise 19: Exploring Rationals

Since you've found that DrScheme seems to represent every real number as a rational, it might be worth finding a way to see what that rational number is. Determine the numerator and denominator of the rational representation of the square root of 2.

Exercise 20: Rounding, Revisited

For small numbers, the `exact->inexact` procedure produces about six digits after the decimal point. Figure out how to get just two digits after the decimal point. You may need to use multiplication, division, and some of the last procedures from the previous exercise.

You need not implement your algorithm; simply come up with one you think will work.

Exercise 21: Wrapup

Quit DrScheme and log out of the workstation.

Notes

Notes on Exercise 12

The `append` procedure joins together the elements of a list to make a new list. Hence, when you append two lists together, the total number of elements in the new list is the sum of the number of elements in the lists.

The `list` procedure creates a new list whose elements are the parameters to `list`. Hence, if `list` takes two parameters, the length of the result is always two, regardless of what those parameters are.

The `cons` procedure builds a new list by placing its first parameter at the start of its second parameter (which is a list). Hence, the length of the result is one more than the length of the second parameter.

Notes on Exercise 18

When you get stuck on this problem, it's probably worth skimming through DrScheme's Help Desk. The numeric operations are documented in section 6.2.5 of the Revised(5) Report on the Algorithmic Language Scheme. (I have not yet put all of these procedures in the Glimmer Scheme Reference.)

History

Wednesday, 22 January 2003 [Samuel A. Rebelsky]

Monday, 27 January 2003 [Samuel A. Rebelsky]

Thursday, 22 January 2004 [Samuel A. Rebelsky]

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 7 09:44:25 2004.
The source to the document was last modified on Thu Jan 22 22:18:57 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Labs/types.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu