CS301 Problem Set 1


This assignment has several goals:

Your assignment is to do Exercises 1 through 6 in Ramsey and Kamin, Chapter 2.

Requirements

As the book says, you should use recursion. Use iteration only if you are stuck.

Put the solution(s) to exercise n in a single file named exn.ic. It's best to use an editor (such as Emacs) that can assist you in editing Lisp or Scheme code. In Emacs, type ESC-X scheme-mode , or ESC-X lisp-mode . (Of course, true Emacs users can have this done automatically based on the file extension.)

Each of the procedures you define should be followed by test cases that are automatically executed when you enter, for example

(use ex1.ic)

Each test case, if successful, should print the value 1 (which is used for "true" in Impcore); if unsuccessful, it should print 0. For example, you could define a test case for Exercise 1 as follows:

(print (= (sigma 10 20) 165))  
This will print 1 if the function sigma applied to 10 and 20 returns 165, 0 otherwise.

Each set of test cases must be preceded by the following Impcore top-level item:

(print 99999)  
This is to help me visually separate the cases when I'm testing. Ideally of course you'd print the name of the function being tested, but Impcore doesn't have any strings!

You must supply a full set of test cases, even (especially!) if you weren't able to get the definition working to your satisfaction. "Full set" means test the "corner cases" (extremes of possible input values) as well as normal cases. For "possible" input values, carefully note the restrictions given in the textbook, as well as my comments below on the individual exercises.

Each file must be consistently and readably formatted, and begin with a comment block like this:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Exercise 2.n
;;; Your name here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Sample format of a solution file

Here is an example of what I want. The file name is ex10.ic (don't go looking for the corresponding Exercise 10, though!

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Exercise 2.10
;;; Haskell Curry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Recursively define gcd(m,n) and lcm(m,n)

(define gcd (m n) 
  (if (= n 0)
       m
       (gcd n (mod m n))))

(print 99999)  

;;; Test cases for gcd

(print (= (gcd 10 10) 10))
(print (= (gcd 7 9) 1))
(print (= (gcd 12 15) 3))
(print (= (gcd 21 144) 3))
(print (= (gcd 1024 256) 256))

;;; lcm
(define lcm (m n)
  (/ (* m n) (gcd m n)))

(print 99999)  

;;; Test cases for lcm

(print (= (lcm 1 4) 4))
(print (= (lcm 4 4) 4))
(print (= (lcm 4 6) 12))
(print (= (lcm 15 12) 60))

Comments on the exercises

You should define auxiliary procedures to help you whenever you need to, but don't proliferate them unnecessarily. These are short programs! You may sometimes want to use the solution to one exercise as part of another; in that case, include (use ex1.ic) (for example) in the solution file.
Adapted from Penny Anderson.