Deque the halls (with my solution)

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
module Data.Deque where

import Data.Foldable
import Data.Monoid
import Data.Traversable
import Control.Applicative

import Control.Comonad       -- http://lpaste.net/107661

import Data.Peano            -- http://lpaste.net/107204

infixr 5 >|
infixl 5 |<

data Deque a = Empty
               | FirstAndLast a
               | Deck a (Deque a) a

(>|) :: a -> Deque a -> Deque a
y >| Empty = FirstAndLast y
y >| (FirstAndLast x) = Deck y Empty x
y >| (Deck a b c) = Deck y (a >| b) c

(|<) :: Deque a -> a -> Deque a
Empty |< x = FirstAndLast x
(FirstAndLast y) |< x = Deck y Empty x
(Deck a b c) |< x = Deck a (b |< c) x

first :: Deque a -> a
first Empty = error "calling first on an empty deque"
first (FirstAndLast x) = x
first (Deck a _ _) = a

last :: Deque a -> a
last Empty = error "calling last on an empty deque"
last (FirstAndLast x) = x
last (Deck _ _ x) = x

isin :: Eq a => a -> Deque a -> Bool
x `isin` Empty = False
x `isin` (FirstAndLast y) = x == y
x `isin` (Deck a d c) = x == a || x == c || x `isin` d

-- Instances ----------------------------------------------------------------

-- Show for prettiness

instance Show a => Show (Deque a) where
   show Empty = "<|>"
   show (FirstAndLast x) = "<|" ++ show x ++ "|>"
   show (Deck a b c) = "<|" ++ show a ++ ", " ++ show' b ++ show c ++ "|>"
      where show' Empty = ""
            show' (FirstAndLast x) = show x ++ ", "
            show' (Deck a b c) = show a ++ ", " ++ show' b ++ show c ++ ", "

{--

... although I suppose if I have a show as above I should make Deque a 
Read instance, to follow the example set by List, but ... eh.

Functor for functoriness (yeah, yeah, I know: but a Deque IS a functor,
so there)

 --}

instance Functor Deque where
   fmap f Empty = Empty
   fmap f (FirstAndLast x) = FirstAndLast $ f x
   fmap f (Deck a b c) = Deck (f a) (fmap f b) (f c)

-- so fmap succ (Empty |< 1 |< 2 |< 3) ~> <|2, 3, 4|>

instance Copointed Deque where -- from the front
   extract Empty = error "Can't extract from empty"
   extract (FirstAndLast x) = x
   extract (Deck a b c) = a -- but should it be the center of b?

-- Foldable so foldr works

instance Foldable Deque where
   foldMap f Empty = mempty
   foldMap f (FirstAndLast x) = f x
   foldMap f (Deck a b c) = f a `mappend` foldMap f b `mappend` f c

-- so Data.Foldable.foldr (+) 0 <|2,3,4|> ~> 9

{-- 

Traversable because, ... well, I SAID SO! And anyway, a language without
the ‘for-loop’ is not Turing-complete (that’s written somewhere on the
internet, so it has to be true), and by making Deque a Traversable instance
we get the for-function ... for FREE!

So there!

 --}

instance Traversable Deque where
   traverse f Empty = pure Empty
   traverse f (FirstAndLast x) = FirstAndLast <$> f x
   traverse f (Deck a b c) = Deck <$> f a <*> traverse f b <*> f c

-- so Data.Traversable.mapM (Just . succ) <|2,3,4|> ~> Just <|3,4,5|>

-- Bonus

-- A bit of destructuring to simply zweitletztes:

dequeFromList :: [a] -> Deque a
dequeFromList [] = Empty
dequeFromList list@(h : t) = dfl list Empty
   where dfl [] deck = deck
         dfl (h : t) deck = dfl t $ deck |< h 

instance Comonad Deque where
   duplicate Empty = Empty :: Deque (Deque a)
   duplicate d@(FirstAndLast x) = FirstAndLast d
   duplicate d@(Deck a b c) = d >| duplicate (b |< c)

{--
nab deck nabs the first element of the deque, returning the element and
the sub-deck-o-cards
 --}

nab :: Deque a -> (a, Deque a)
nab Empty = error "calling nab on an empty deque"
nab (FirstAndLast x) = (x, Empty)
nab (Deck a b c) = (a, b |< c)

-- and extension of nab:

takeD :: Int -> Deque a -> [a]
takeD n Empty = []
takeD n deck = t' (fromInt n) (nab deck)
   where t' Z _ = []
         t' (S n) (h, Empty) = [h]
         t' (S n) (h, deck) = h : t' n (nab deck)

takeWhileD :: (a -> Bool) -> Deque a -> [a]
takeWhileD fn Empty = []
takeWhileD fn deck = t' (nab deck)
   where t' (h, rest) = if fn h then h : t' (nab rest) else []

dropWhileD :: (a -> Bool) -> Deque a -> Deque a
dropWhileD fn Empty = Empty
dropWhileD fn deck = d' (nab deck) deck
   where d' (h, rest) deck = if fn h then dropWhileD fn rest else deck

-- the different result types of takeWhileD and dropWhileD may
-- come back to haunt me, but for now ... *shrugs*

{--
ban is the dual of nab, nabbing the tush of the deck and returning the
deck, sans tush.
 --}

ban :: Deque a -> (a, Deque a)
ban Empty = error "calling ban on an empty deque"
ban (FirstAndLast x) = (x, Empty)
ban (Deck a b c) = (c, a >| b)

-- zweitletztes returns the penultimate element of the deck

zweitletztes :: Deque a -> a
zweitletztes deck = Data.Deque.last $ snd $ ban deck

{--

Proof that zweitletztes deck is faster than zweitletztes [a]:

1. ban deck: 1 step
2. snd ... okay, really? 1 step? ... it's more like a destructuring, but whatever
3. last deck: 1 step

A fast-ish implementation of zweitletztes for [a]:

zweitletztes = reverse >>> tail >>> head

1. reverse list: n steps (ouch)
2. tail list: 1 step
3. head list: 1 step

At LEAST zweitletztes takes 3 steps for a singleton [a]
Otherwise it takes n + 2 steps where n > 1.

For the implementation of zweitletztes in the problem statement, which was:

zweitletztes = last . init

it's even worse:

1. init list: n - 1 steps
2. last it:   n - 1 steps

The given implementation of zweitletztes takes 2 * (n - 1) steps.

zweitletztes deck is faster than either implementation of zweitletztes [a]

Q.E.D.

 --}
112:10: Warning: Use foldl
Found:
dfl [] deck = deck
dfl (h : t) deck = dfl t $ deck |< h
Why not:
dfl t deck = foldl (|<) deck t