Skip to content

Commit f3711e3

Browse files
committed
Fixup templates
1 parent d6b75ba commit f3711e3

File tree

13 files changed

+308
-349
lines changed

13 files changed

+308
-349
lines changed

hw1/src/HW1/T1.hs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
module HW1.T1
2-
( Day (..),
3-
nextDay,
4-
afterDays,
5-
isWeekend,
6-
daysToParty
7-
) where
2+
( Day (..)
3+
, afterDays
4+
, daysToParty
5+
, isWeekend
6+
, nextDay
7+
) where
88

99
import GHC.Natural (Natural)
1010

11-
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving Show
11+
data Day =
12+
Monday
13+
| Tuesday
14+
| Wednesday
15+
| Thursday
16+
| Friday
17+
| Saturday
18+
| Sunday
1219

1320
-- | Returns the day that follows the day of the week given as input.
1421
nextDay :: Day -> Day
1522

16-
1723
-- | Returns the day of the week after a given number of days has passed.
1824
afterDays :: Natural -> Day -> Day
1925

2026
-- | Checks if the day is on the weekend.
2127
isWeekend :: Day -> Bool
2228

23-
24-
25-
-- | Computes the number of days until the next Friday.
29+
-- | Computes the number of days until Friday.
2630
daysToParty :: Day -> Natural

hw1/src/HW1/T2.hs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
module HW1.T2
2-
( N(..),
3-
nplus,
4-
nmult,
5-
nsub,
6-
ncmp,
7-
nFromNatural,
8-
nToNum,
9-
--advanced (comment if don't want to do andvanced)
10-
nEven,
11-
nOdd,
12-
ndiv,
13-
nmod
14-
) where
2+
( N (..)
3+
, nFromNatural
4+
, nToNum
5+
, ncmp
6+
, nmult
7+
, nplus
8+
, nsub
9+
-- * Advanced
10+
, nEven
11+
, nOdd
12+
, ndiv
13+
, nmod
14+
) where
1515

1616
import GHC.Natural (Natural)
1717

18-
data N = Z | S N deriving Show
19-
18+
data N =
19+
Z
20+
| S N
2021

2122
nplus :: N -> N -> N -- addition
2223

23-
2424
nmult :: N -> N -> N -- multiplication
2525

26-
2726
nsub :: N -> N -> Maybe N -- subtraction (Nothing if result is negative)
2827

29-
30-
3128
ncmp :: N -> N -> Ordering -- comparison (Do not derive Ord)
3229

33-
34-
3530
nFromNatural :: Natural -> N
3631

3732
nToNum :: Num a => N -> a
3833

39-
40-
-- Advanced
34+
-- | Advanced
4135

4236
nEven, nOdd :: N -> Bool -- parity checking
4337

44-
4538
ndiv :: N -> N -> N -- integer division
4639

47-
4840
nmod :: N -> N -> N -- modulo operation

hw1/src/HW1/T3.hs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
module HW1.T3
2-
( Tree (..),
3-
tsize,
4-
tdepth,
5-
tmember,
6-
tinsert,
7-
tFromList
8-
) where
2+
( Tree (..)
3+
, tFromList
4+
, tdepth
5+
, tinsert
6+
, tmember
7+
, tsize
8+
) where
9+
10+
import GHC.Generics
11+
12+
-- The Meta field must store additional information about the subtree that can be accessed in constant time, for example its size. You can use Int, (Int, Int), or a custom data structure:
13+
--
14+
-- type Meta = Int -- OK
15+
-- data Meta = M Int Int -- OK
16+
17+
data Tree a
18+
= Leaf
19+
| Branch
20+
Meta
21+
(Tree a)
22+
a
23+
(Tree a)
924

10-
data Tree a = Leaf | Branch (Int, Int) (Tree a) a (Tree a) deriving Show
1125

1226
-- | Size of the tree, O(1).
1327
tsize :: Tree a -> Int
1428

1529
-- | Depth of the tree.
1630
tdepth :: Tree a -> Int
1731

18-
1932
-- | Check if the element is in the tree, O(log n)
2033
tmember :: Ord a => a -> Tree a -> Bool
2134

22-
23-
-- | Build a tree from a list, O(n log n)
24-
tFromList :: Ord a => [a] -> Tree a
25-
26-
27-
2835
-- | Insert an element into the tree, O(log n)
2936
tinsert :: Ord a => a -> Tree a -> Tree a
3037

31-
-- support functions
38+
-- | Build a tree from a list, O(n log n)
39+
tFromList :: Ord a => [a] -> Tree a
3240

33-
mkBranch :: Tree a -> a -> Tree a -> Tree a
41+
-- Tip 1: in order to maintain the CachedSize invariant, define a helper function:
42+
--
43+
-- mkBranch :: Tree a -> a -> Tree a -> Tree a
44+
-- Tip 2: the Balanced invariant is the hardest to maintain, so implement it last. Search for “tree rotation”.

hw1/src/HW1/T4.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module HW1.T4
2-
( tfoldr
3-
) where
2+
( tfoldr
3+
) where
44

5-
import HW1.T3 ( Tree(..) )
5+
import HW1.T3 (Tree (Branch, Leaf))
66

77
tfoldr :: (a -> b -> b) -> b -> Tree a -> b
88

9-
-- treeToList :: Tree a -> [a] -- output list is sorted
10-
-- treeToList = tfoldr (:) []
9+
treeToList :: Tree a -> [a] -- output list is sorted
10+
treeToList = tfoldr (:) []

hw1/src/HW1/T5.hs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
module HW1.T5
2-
( splitOn,
3-
joinWith
4-
) where
2+
( joinWith
3+
, splitOn
4+
) where
55

6-
import Data.List.NonEmpty ( NonEmpty(..) )
6+
import Data.List.NonEmpty (NonEmpty ((:|)))
77

88
splitOn :: Eq a => a -> [a] -> NonEmpty [a]
99

10-
11-
-- ghci> splitOn '/' "path/to/file"
12-
-- ["path", "to", "file"]
13-
14-
-- ghci> splitOn '/' "path/with/trailing/slash/"
15-
-- ["path", "with", "trailing", "slash", ""]
16-
1710
joinWith :: a -> NonEmpty [a] -> [a]
18-
19-
-- (joinWith sep . splitOn sep) ≡ id
20-
21-
-- ghci> "import " ++ joinWith '.' ("Data" :| "List" : "NonEmpty" : [])
22-
-- "import Data.List.NonEmpty"

hw1/src/HW1/T6.hs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
module HW1.T6
2-
( mcat,
3-
epart
4-
) where
5-
2+
( epart
3+
, mcat
4+
) where
65

76
mcat :: Monoid a => [Maybe a] -> a
87

9-
-- using foldable
10-
11-
-- ghci> mcat [Just "mo", Nothing, Nothing, Just "no", Just "id"]
12-
-- "monoid"
13-
14-
-- ghci> Data.Monoid.getSum $ mcat [Nothing, Just 2, Nothing, Just 40]
15-
-- 42
16-
17-
-- | Concat all Either's to array
18-
198
epart :: (Monoid a, Monoid b) => [Either a b] -> (a, b)
20-
21-
-- ghci> epart [Left (Sum 3), Right [1,2,3], Left (Sum 5), Right [4,5]]
22-
-- (Sum {getSum = 8},[1,2,3,4,5])

hw1/src/HW1/T7.hs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
module HW1.T7
2-
( ListPlus(..),
3-
Inclusive(..),
4-
DotString(..),
5-
Fun(..)
6-
) where
7-
-- Define the following data type and a lawful Semigroup instance for it
8-
9-
data ListPlus a = a :+ ListPlus a | Last a deriving Show
2+
( DotString (..)
3+
, Fun (..)
4+
, Inclusive (..)
5+
, ListPlus (..)
6+
) where
7+
8+
data ListPlus a =
9+
a :+ ListPlus a
10+
| Last a
1011
infixr 5 :+
1112

1213
instance Semigroup (ListPlus a) where
14+
...
1315

14-
15-
data Inclusive a b = This a | That b | Both a b deriving Show
16+
data Inclusive a b =
17+
This a
18+
| That b
19+
| Both a b
1620

1721
instance (Semigroup a, Semigroup b) => Semigroup (Inclusive a b) where
22+
...
1823

19-
20-
21-
newtype DotString = DS String deriving Show
24+
newtype DotString = DS String
2225

2326
instance Semigroup DotString where
24-
25-
-- ghci> DS "person" <> DS "address" <> DS "city"
26-
-- DS "person.address.city"
27-
28-
-- Implement a Monoid instance for it
29-
30-
-- mempty <> a ≡ a
31-
-- a <> mempty ≡ a
27+
...
3228

3329
instance Monoid DotString where
30+
...
3431

35-
36-
-- Implement lawful Semigroup and Monoid instances for it
3732
newtype Fun a = F (a -> a)
3833

3934
instance Semigroup (Fun a) where
35+
...
4036

4137
instance Monoid (Fun a) where
38+
...

0 commit comments

Comments
 (0)