1
1
---
2
- title: Homework #1, Due Monday 1/26 /15
2
+ title: Homework #1, Due Monday 1/25 /15
3
3
---
4
4
5
+ Preliminaries
6
+ -------------
7
+
8
+ Before starting this assignment:
9
+
10
+ 1. Download and install [Stack](http://docs.haskellstack.org/en/stable/README.html#how-to-install)
11
+ 2. Download the assignment [code bundle](/static/hw1.tar.gz).
12
+ 3. Unpack bundle `tar -zxvf hw1.tar.gz`
13
+ 3. Verify that things build etc. by:
14
+
15
+ ~~~
16
+ $ stack build
17
+ ... ( wait for a long time )
18
+ $ stack ghci Hw1
19
+ ...
20
+ *Hw1> :l SOE/src/Draw.lhs
21
+ ...
22
+ *Draw> main0
23
+ ~~~
24
+
25
+ You should see a window with some shapes in it. Now if you do:
26
+
27
+ ~~~
28
+ > : l Main. hs
29
+ > main
30
+ ~~~
31
+
32
+ You should get an exception:
33
+
34
+ ~~~
35
+ *** Exception: Define me!
36
+ ~~~
37
+
38
+ Interacting with GHCi
39
+ ---------------------
40
+
41
+ 1. Keep open a `ghci` (by running `stack ghci Hw1` in the command prompt),
42
+ 2. Load `Hw1.hs` by typing `:l Hw1.hs`
43
+ 3. Edit `Hw1.hs` to implement the various functions,
44
+ 4. After each edit, to test your code, do `:r` and test the function.
45
+
46
+ There are fancy modes for various editors (vim, emacs, atom, sublime)
47
+ that make the above even smoother. Feel free to investigate to your taste.
48
+
49
+
5
50
Haskell Formalities
6
51
-------------------
7
52
@@ -22,40 +67,10 @@ strings below
22
67
> myEmail = " Write Your Email Here"
23
68
> mySID = " Write Your SID Here"
24
69
25
-
26
- Preliminaries
27
- -------------
28
-
29
- Before starting this assignment:
30
-
31
- * Download and install the [Haskell Platform](http://www.haskell.org/platform/).
32
- * Download the [SOE code bundle](/static/SOE-cse230.tar.gz).
33
-
34
- * Verify that it works by changing into the `SOE/src` directory and
35
- running `ghci Draw.lhs`, then typing `main0` at the prompt:
36
-
37
- ~~~
38
- cd SOE/src
39
- ghci Draw.lhs
40
- *Draw> main0
41
- ~~~
42
-
43
- You should see a window with some shapes in it.
44
-
45
- **NOTE:** If you have trouble installing SOE, [see this page](soe-instructions.html)
46
-
47
- 5. Download the required files for this assignment: [hw1.tar.gz](/static/hw1.tar.gz).
48
- Unpack the files and make sure that you can successfully run the main program (in `Main.hs`).
49
- We've provided a `Makefile`, which you can use if you like. You should see this output:
50
-
51
- ~~~
52
- Main: Define me!
53
- ~~~
54
-
55
70
Part 1: Defining and Manipulating Shapes
56
71
----------------------------------------
57
72
58
- You will write all of your code in the `hw1.lhs ` file, in the spaces
73
+ You will write all of your code in the `Hw1.hs ` file, in the spaces
59
74
indicated. Do not alter the type annotations --- your code must
60
75
typecheck with these types to be accepted.
61
76
@@ -66,8 +81,8 @@ The following are the definitions of shapes:
66
81
> | RtTriangle Side Side
67
82
> | Polygon [Vertex ]
68
83
> deriving Show
69
- >
70
- > type Radius = Float
84
+ >
85
+ > type Radius = Float
71
86
> type Side = Float
72
87
> type Vertex = (Float , Float )
73
88
@@ -76,10 +91,10 @@ The following are the definitions of shapes:
76
91
built with the Polygon constructor.
77
92
78
93
> rectangle :: Side -> Side -> Shape
79
- > rectangle = error " Define me!"
94
+ > rectangle = error " Define me!"
80
95
81
96
> rtTriangle :: Side -> Side -> Shape
82
- > rtTriangle = error " Define me!"
97
+ > rtTriangle = error " Define me!"
83
98
84
99
2. Define a function
85
100
@@ -104,15 +119,15 @@ The following are the definitions of shapes:
104
119
To solve the puzzle, you must move all the discs from the starting peg
105
120
to another by moving only one disc at a time and never stacking
106
121
a larger disc on top of a smaller one.
107
-
122
+
108
123
To move $ n$ discs from peg $ a$ to peg $ b$ using peg $ c$ as temporary storage:
109
-
124
+
110
125
1. Move $ n - 1 $ discs from peg $ a$ to peg $ c$ .
111
126
2. Move the remaining disc from peg $ a$ to peg $ b$ .
112
127
3. Move $ n - 1 $ discs from peg $ c$ to peg $ b$ .
113
-
128
+
114
129
Write a function
115
-
130
+
116
131
> hanoi :: Int -> String -> String -> String -> IO ()
117
132
> hanoi = error " Define me!"
118
133
@@ -123,7 +138,7 @@ The following are the definitions of shapes:
123
138
124
139
should emit the text
125
140
126
- ~~~
141
+ ~~~
127
142
move disc from a to c
128
143
move disc from a to b
129
144
move disc from c to b
@@ -192,12 +207,12 @@ Now write a *non-recursive* version of the above.
192
207
`addEachPair [(1,2), (20,21), (300,301)]` should return `[3,41,601]`
193
208
194
209
> addEachPair :: [(Int , Int )] -> [Int ]
195
- > addEachPair = error " Define me!"
210
+ > addEachPair = error " Define me!"
196
211
197
212
Now write a *non-recursive* version of the above.
198
213
199
214
> addEachPairNonRecursive :: [(Int , Int )] -> [Int ]
200
- > addEachPairNonRecursive = error " Define me!"
215
+ > addEachPairNonRecursive = error " Define me!"
201
216
202
217
`minList` should return the *smallest* value in the list. You may assume the
203
218
input list is *non-empty*.
@@ -232,7 +247,7 @@ So: `fringe (Branch (Leaf 1) (Leaf 2))` should return `[1,2]`
232
247
> fringe :: Tree a -> [a ]
233
248
> fringe = error " Define me!"
234
249
235
- `treeSize` should return the number of leaves in the tree.
250
+ `treeSize` should return the number of leaves in the tree.
236
251
So: `treeSize (Branch (Leaf 1) (Leaf 2))` should return `2`.
237
252
238
253
> treeSize :: Tree a -> Int
@@ -262,7 +277,7 @@ should return `(IBranch 1 (IBranch 2 ILeaf ILeaf) ILeaf)`.
262
277
263
278
> takeTreeWhile :: (a -> Bool ) -> InternalTree a -> InternalTree a
264
279
> takeTreeWhile = error " Define me!"
265
-
280
+
266
281
Write the function map in terms of foldr:
267
282
268
283
> myMap :: (a -> b ) -> [a ] -> [b ]
@@ -324,7 +339,7 @@ this one value for purposes of this assignment. The XML value in
324
339
information as `Play.hs`. You may want to have a look at it in your
325
340
favorite browser. The HTML in `sample.html` has the following structure
326
341
(with whitespace added for readability):
327
-
342
+
328
343
~~~
329
344
<html>
330
345
<body>
@@ -370,13 +385,13 @@ to `sample.html`.
370
385
>
371
386
> firstDiff :: Eq a => [a ] -> [a ] -> Maybe ([a ],[a ])
372
387
> firstDiff [] [] = Nothing
373
- > firstDiff (c: cs) (d: ds)
374
- > | c== d = firstDiff cs ds
388
+ > firstDiff (c: cs) (d: ds)
389
+ > | c== d = firstDiff cs ds
375
390
> | otherwise = Just (c: cs, d: ds)
376
391
> firstDiff cs ds = Just (cs,ds)
377
- >
392
+ >
378
393
> testResults :: String -> String -> IO ()
379
- > testResults file1 file2 = do
394
+ > testResults file1 file2 = do
380
395
> f1 <- readFile file1
381
396
> f2 <- readFile file2
382
397
> case firstDiff f1 f2 of
@@ -420,10 +435,11 @@ Submission Instructions
420
435
421
436
* If working with a partner, you should both submit your assignments
422
437
individually.
423
- * Make sure your `hw1.lhs` is accepted by GHC without errors or warnings.
438
+
439
+ * Make sure your `Hw1.hs` is accepted by GHCi without errors or warnings.
440
+
424
441
* Attach your `hw1.hs` file in an email to `
[email protected] ` with the
425
- subject "HW1" (minus the quotes).
426
- *This address is unmonitored!*
442
+ subject "HW1" (minus the quotes). *This address is unmonitored!*
427
443
428
444
Credits
429
445
-------
0 commit comments