Problem
I'm trying to implement the modified Dragon Curve from AoC Day 16 as an infinite list in Haskell.
The list is composed of True
and False
. We start with some list s0
:
s1 = s0 ++ [False] ++ (map not . reverse) s0
s2 = s1 ++ [False] ++ (map not . reverse) s1
s3 = s2 ++ [False] ++ (map not . reverse) s2
Generally
sn = s(n-1) ++ [0] ++ (map not . reverse) s(n-1)
= s0 ++ [0] ++ (f s0) ++ [0] ++ (f (s0 ++ [0] ++ (f s0))) ++ ...
where f = (map not . reverse)
Attempted Implementation
I can get sn
quite easily using the iterate
function.
modifiedDragonCurve :: [Bool] -> Int -> [Bool]
modifiedDragonCurve s n = (iterate f s)!!n
where f s = s ++ [False] ++ (map not . reverse) s
This gives me a list [s0, s1, s2, ...]
. However, since s(n-1)
is a prefix of sn
this could be built as an infinite list, but i cannot figure out how to approach it. I think I need something along the lines of
modifiedDragonCurve :: [Bool] -> [Bool]
modifiedDragonCurve s = s ++ [False] ++ (map not . reverse) listSoFar
But cannot figure out how to refer to the already generated list (listSoFar
).
Any suggestions would be greatly appreciated.