CS 1101 (A05) Homework 1: Warming up in Scheme

Due: August 30 (Tuesday) at 11:59pm via turnin (assignment name hwk1).

Assignment Goals

• To make sure you can write simple Scheme programs.
• To make sure you can evaluate simple Scheme programs.

The Assignment

Part 1: Evaluating Scheme Programs

Show the steps that Scheme would take to evaluate each of the following expressions. In each expression, indicate the subexpression that is evaluated to obtain the next expression. For example:

```        (sqrt (+ (* 3 3) (* 4 4)))
^^^^^^^
= (sqrt (+ 9 (* 4 4)))
^^^^^^^
= (sqrt (+ 9 16))
^^^^^^^^
= (sqrt 25)
^^^^^^^^^
= 5
```

Put your answers in comments in the file for your assignment. You may use the Stepper to check your answers, but do the problem manually first to make sure you understand how Scheme works.

1. `(* (+ 4 5) (* (- 9 3) 7))`

2. ` (/ (- (* 16 16) (double 3)) 2)` where double is defined as `(define (double n) (* n 2))`

3. `(weeks-to-days (years-to-weeks 3.5))` where the functions are defined as:

```(define (weeks-to-days weeks) (* weeks 7)) (define (years-to-weeks years) (* years 52.25)) ```
``` ```
``` Part 2: Composing Functions over Images This set of questions has you create images of fabrics and items of clothing printed in those fabrics. These exercises build on some images and operators specific to these problems. I've provided them for you in a bundle of code called a teachpack. Download the teachpack to your computer, then use the Add Teachpack option under the Language menu to load the teachpack contents into DrScheme. (You'll be asked to select the teachpack file you downloaded -- post if you have questions on how to do this). A description of functions and images in the teachpack. In the problems below, the tshirt and hat images (from the teachpack) are images of clothing. The worm, chili, and flower images are good for logos and for creating print fabrics. Write an expression to create blue fabric with red stripes in one direction and purple stripes in the other (you pick the size). Write an expression to create a small green stripe fabric with a chili print on it (you pick the size). Invent two fabrics of your own using the teachpack functions. Each fabric should be the same size as the tshirt image. Use the scheme functions image-height and image-width to figure out the size of a tshirt. You can create your own print images using the circle and rectangle operators from the image teachpack (they are included in this teachpack). Each fabric should involve at least two calls to functions that create fabrics. Be creative! Write a function create-from-fabric that consumes an image for a white clothing item and fabric sized to fit that item and produces an image of the clothing item in the given fabric. [HINT: Recall what overlay/xy does with white parts of images--check the DrScheme helpdesk if necessary.] Write a function add-center-logo that consumes an image for a logo and an image for a clothing item and produces an image of the clothing item with the logo centered on it. The given clothing item may already be printed in fabric. Write a function stack-logos that consumes images of two logos and returns an image with the first logo just above the second logo. You do not need to center the logos, but you may if you wish. Note that Scheme has a function max that consumes two numbers and produces the larger of the numbers. Write a function side-by-side-logos that consumes images of two logos and returns an image with the first logo to the left of the second logo. You do not need to center the logos, but you may if you wish. Write expressions that create three pieces of clothing using a combination of the teachpack functions and your definitions. You should use each of the functions you defined at least once. Again, be creative! What to Turn In Turn in a single file hwk1.ss or hwk1.scm containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file. Help! How Do I Get Started? Do the problems in Part 1 first, to make sure you see how Scheme works. For part 2, start by loading the teachpack into DrScheme. Familiarize yourself with the images and functions in the teachpack. Enter the names of the images one at a time in the interactions window. Try the functions that create fabric images (create-solid-fabric, add-print, add-horiz-stripe, and add-vertical-stripe) to see what each one does. Post other questions to the discussion board or come to office hours. Back to the Assignments page ```