Docsity

Computer Science - Homework 2 Solutions | CIS 252, Assignments of Computer Science

Toggle sidebar
bg1
Homework 2 CIS 252 (Spring 2009) — Introduction to Computer Science
Coverage
This assignment covers material through Chapter 4 of Haskell: The Craft of Func-
tional Programming.
Logistics
This homework is officially due in the bin in CST 3-212 by noon on Thursday,
February 5. However, it comes with an automatic extension: anything submit-
ted by noon on Friday, February 6 will be accepted as being on time.
You may work singly or in pairs on this assignment.
What to turn in
You should turn in a hard copy of your source code and a transcript demon-
strating convincingly that your code is correct. You should also submit your
code (but not the transcript) electronically, as described on the course web site.
You should follow the design recipe and documentation requirements.How-
ever, you do not need to include examples or test cases in your code (they’d be
way too big). Instead, simply write “See the Homework 3 write-up for exam-
ples” in the EXAMPLES part of your comments.
Exercises
You may use any code from lecture that you wish: in fact, we encourage you to do
so. However, you should include a note in your comments indicating that you
are doing so and specifying which functions you are reusing.
You will again need the file Pictures.lhs to use the Pictures module. Do not add
your code to this file. Instead, you should include an import Pictures directive
at the top of your file.
1. Write a Haskell function altThree
altThree :: Int -> Picture -> Picture -> Picture -> Picture
such that altThree n pic1 pic2 pic3 returns a row of npictures, as fol-
lows: every odd-indexed picture is pic1, and the even-indexed pictures
cycle between pic2 and pic3.
2. Write a Haskell function
indexRow :: Int -> Int -> Picture -> Picture -> Picture
such that indexRow c n pic1 pic2 returns a row of npictures: pic1 is at
index c, and pic2 occurs everywhere else. Your code should raise an error
if either cor nis less than 1. Note: You may assume that cis never greater than
n.
3. Write a Haskell function
stripes :: Int -> Int -> Picture -> Picture -> Picture
such that stripes m n pic1 pic2 returns an m×ngrid of pictures, with
pic1 appearing on the 1st and mth rows, as well in all odd-numbered
columns; pic2 should appear everywhere else. Your code should raise
an error if either mor nis less than 1, or if nis even.
4. Write a Haskell function
leftTri :: Int -> Picture -> Picture -> Picture
such that leftTri n pic1 pic2 returns an n×ngrid, as follows:
•All pictures on the right half of the grid are pic2. (The only exception:
if nis odd, then the very center picture on the grid is pic1.)
•Pictures on the left half of the grid on either of the two diagonals, or
that fall between the two diagonals, are pic1.
•All other pictures are pic2.
Your code should raise an error if nis less than 1.
Final comments: For the last question especially, I strongly encourage you to
think out your solutions with paper first, before sitting down to write code. Also,
if any of your functions is longer than about five lines (not counting helper func-
tions), then you’re probably making things more complicated than they need to
be.
Page 1
pf3

Partial preview of the text

Download Computer Science - Homework 2 Solutions | CIS 252 and more Assignments Computer Science in PDF only on Docsity!

Coverage

This assignment covers material through Chapter 4 of Haskell: The Craft of Func- tional Programming.

Logistics

This homework is officially due in the bin in CST 3-212 by noon on Thursday, February 5. However, it comes with an automatic extension: anything submit- ted by noon on Friday, February 6 will be accepted as being on time.

You may work singly or in pairs on this assignment.

What to turn in

You should turn in a hard copy of your source code and a transcript demon- strating convincingly that your code is correct. You should also submit your code (but not the transcript) electronically , as described on the course web site.

You should follow the design recipe and documentation requirements. How- ever, you do not need to include examples or test cases in your code (they’d be way too big). Instead, simply write “See the Homework 3 write-up for exam- ples” in the EXAMPLES part of your comments.

Exercises

You may use any code from lecture that you wish: in fact, we encourage you to do so. However, you should include a note in your comments indicating that you are doing so and specifying which functions you are reusing.

You will again need the file Pictures.lhs to use the Pictures module. Do not add your code to this file. Instead, you should include an import Pictures directive at the top of your file.

  1. Write a Haskell function altThree

altThree :: Int -> Picture -> Picture -> Picture -> Picture

such that altThree n pic1 pic2 pic3 returns a row of n pictures, as fol- lows: every odd-indexed picture is pic1, and the even-indexed pictures cycle between pic2 and pic3.

  1. Write a Haskell function

indexRow :: Int -> Int -> Picture -> Picture -> Picture

such that indexRow c n pic1 pic2 returns a row of n pictures: pic1 is at index c, and pic2 occurs everywhere else. Your code should raise an error if either c or n is less than 1. Note: You may assume that c is never greater than n.

  1. Write a Haskell function

stripes :: Int -> Int -> Picture -> Picture -> Picture

such that stripes m n pic1 pic2 returns an m × n grid of pictures, with pic1 appearing on the 1st^ and mth^ rows, as well in all odd-numbered columns; pic2 should appear everywhere else. Your code should raise an error if either m or n is less than 1, or if n is even.

  1. Write a Haskell function

leftTri :: Int -> Picture -> Picture -> Picture

such that leftTri n pic1 pic2 returns an n × n grid, as follows:

  • All pictures on the right half of the grid are pic2. (The only exception: if n is odd, then the very center picture on the grid is pic1.)
  • Pictures on the left half of the grid on either of the two diagonals, or that fall between the two diagonals, are pic1.
  • All other pictures are pic2.

Your code should raise an error if n is less than 1.

Final comments: For the last question especially, I strongly encourage you to think out your solutions with paper first, before sitting down to write code. Also, if any of your functions is longer than about five lines (not counting helper func- tions), then you’re probably making things more complicated than they need to be.

Examples

The following examples make use of these definitions of white, black, blob, and exx used in lecture.

Main> printPicture (altThree 10 white blob exx) .......#.......#...#.......#.......#...#.......#.. ......###.......#.#.......###.......#.#.......###. ......###........#........###........#........###. ......###.......#.#.......###.......#.#.......###. .......#.......#...#.......#.......#...#.......#..

Main> printPicture (altThree 7 white blob exx) .......#.......#...#.......#....... ......###.......#.#.......###...... ......###........#........###...... ......###.......#.#.......###...... .......#.......#...#.......#.......

Main> printPicture (indexRow 3 7 white blob) ..#....#.........#....#....#....#.. .###..###.......###..###..###..###. .###..###.......###..###..###..###. .###..###.......###..###..###..###. ..#....#.........#....#....#....#..

Main> printPicture (indexRow 7 7 white blob) ..#....#....#....#....#....#....... .###..###..###..###..###..###...... .###..###..###..###..###..###...... .###..###..###..###..###..###...... ..#....#....#....#....#....#.......

Main> printPicture (indexRow 1 7 white blob) .......#....#....#....#....#....#.. ......###..###..###..###..###..###. ......###..###..###..###..###..###. ......###..###..###..###..###..###. .......#....#....#....#....#....#..

Main> printPicture (stripes 6 7 blob white) ..#....#....#....#....#....#....#.. .###..###..###..###..###..###..###. .###..###..###..###..###..###..###. .###..###..###..###..###..###..###. ..#....#....#....#....#....#....#.. ..#.........#.........#.........#.. .###.......###.......###.......###. .###.......###.......###.......###. .###.......###.......###.......###. ..#.........#.........#.........#.. ..#.........#.........#.........#.. .###.......###.......###.......###. .###.......###.......###.......###. .###.......###.......###.......###. ..#.........#.........#.........#.. ..#.........#.........#.........#.. .###.......###.......###.......###. .###.......###.......###.......###. .###.......###.......###.......###. ..#.........#.........#.........#.. ..#.........#.........#.........#.. .###.......###.......###.......###. .###.......###.......###.......###. .###.......###.......###.......###. ..#.........#.........#.........#.. ..#....#....#....#....#....#....#.. .###..###..###..###..###..###..###. .###..###..###..###..###..###..###. .###..###..###..###..###..###..###. ..#....#....#....#....#....#....#..