-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLec04.hs
77 lines (60 loc) · 1.81 KB
/
Lec04.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-# LANGUAGE MultiParamTypeClasses #-}
module Lec04 where
import Data.Functor
import Data.Monoid
{-
class Eq a where
(==) :: a -> a -> Bool
x == y = not (x /= y)
(/=) :: a -> a -> Bool
x /= y = not (x == y)
-}
data Foo = F Int
| G Bool
deriving (Show)
instance Eq Foo where
F x == F y = x == y
G x == G y = x == y
_ == _ = False
f :: String -> String
f = show . (read :: String -> Int)
class Castable a b where
cast :: a -> b
instance Castable Int Integer where
cast = fromIntegral
maybeConcat :: Maybe String -> Maybe String -> Maybe String
maybeConcat (Just s1) (Just s2) = Just $ s1 ++ s2
maybeConcat Nothing (Just s) = Just s
maybeConcat (Just s) Nothing = Just s
maybeConcat Nothing Nothing = Nothing
data X a = X a
deriving (Show)
instance Functor X where
fmap f (X a) = X $ f a
-- Style Examples -------------------------------------
isSorted :: Ord a => [a] -> Bool
isSorted [] = True
isSorted [x] = True
isSorted (x1 : x2 : xs)
| x1 <= x2 = isSorted (x2 : xs)
| otherwise = False
-- The original function had:
-- * Redundant base cases
-- * Unnecessary guards
isSorted' :: Ord a => [a] -> Bool
isSorted' (x1 : x2 : xs) = x1 <= x2 && isSorted' (x2 : xs)
isSorted' _ = True
countMistakes :: Eq a => [a] -> [[a]] -> [Int]
countMistakes ans [] = []
countMistakes ans (r:rs) =
countMs ans r : countMistakes ans rs
where countMs [] _ = 0
countMs _ [] = 0
countMs (x:xs) (y:ys)
| x == y = countMs xs ys
| otherwise = 1 + countMs xs ys
-- The original version had:
-- * Explicit recursion patterns that can be replaced with `map` and `zipWith`
countMistakes' :: Eq a => [a] -> [[a]] -> [Int]
countMistakes' ans = map countMs
where countMs = sum . zipWith (fromEnum .: (/=)) ans