Homework 2

This is the archived website of SI 413 from the Fall 2012 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Note: Many of these exercises are programming exercises, but you do not need to submit them electronically. Everything should be turned in in one packet, all printed out for me to see.

# 1 List Basics

1. Using only cons, null, car, and cdr, write an expression to produce the nested list

'(3 (4 5) 6).

2. Write a simple quoted expression that is equivalent to

(cons (cons 3 (cons 'q null)) (cons 'a null)).

3. Using only cons, null, car, and cdr, write a function (get2nd L) that takes a list L and returns the second element in the list.

# 2 Split Digits

Write a recursive function split-digits that takes a number n and returns a list with the digits of n, in reverse.

For example, (split-digits 413) should produce the list '(3 1 4).

# 3 Append

Write a function called my-append which has the same behavior as the append function built-in to Scheme. (Your function only needs to handle the case when there are exactly two arguments, and both are lists.)

For example, calling (my-append '(a b c) '(d e)) should produce '(a b c d e).

# 4 Count Down

Write a function called count-down which takes a positive integer n and produces a list with the integers from n down to 1, in that order.

For example (count-down 4) should produce the list '(4 3 2 1).

# 5 Symbol Mixup

Write a function (mixup x) that takes an argument x, which can be any symbol or any number, and produces the opposite type of thing, either the number 5 if x is a symbol, or the symbol 'num if x is a number.

For example, (mixup 20) should produce 'num, and (mixup 'hello) should produce 5.

# 6 Nested Quotes

When you type 5 into the interpreter, it returns 5.

When you type (quote 5), it still returns the number 5.

But when you type (quote (quote 5)) or ''5, it returns '5.

What do you think is going on here? Why do you need two quotes to make the symbol 5?

(Caution: this is pretty tricky. Think about how evaluation works. Play around, experiment, discuss.)

# 7 Building Blocks

In the C programming language, give an example of each of the following types of code fragments.

1. An atom (or literal)
2. A value that is not an atom
3. An expression that is not a value
4. A statement that does not end in a semicolon