I have been feeling very stupid (took me like 8 hours) trying to implement a doubly linked circular list. Is there a better way to implement fromNonEmpty
?
data DLCL a = DLCL {value :: a, prev :: DLCL a, next :: DLCL a}
instance Show a => Show (DLCL a) where
show (DLCL x (DLCL y _ _) (DLCL z _ _)) = show y <> " <- " <> show x <> " -> " <> show z
fromNonEmpty :: NonEmpty a -> DLCL a
fromNonEmpty (x:|xs) = h
where
h = DLCL x l f
(l, f) = fromList h h xs
fromList f p = \case
[x] -> let c = DLCL x p f
in (c, c)
x:xs -> let c = DLCL x p n
(l,n) = fromList f c xs
in (l,c)
[] -> (p,p)
Also, how does the GC collect this? Does it look for isolated networks?