fn impl(rest: List(a), first: a, prev: a) -> #(List(#(a, #(a, a))), a) { case rest { [] -> #([], first) [x] -> #([#(x, #(prev, first))], x) [x, y, ..xs] -> { let #(next_list, last) = impl([y, ..xs], first, x) #([#(x, #(prev, y)), ..next_list], last) } } } pub fn find_neighbours(sites: List(a)) -> List(#(a, #(a, a))) { case sites { [] -> [] [x] -> [#(x, #(x, x))] [x, y] -> [#(x, #(y, y)), #(y, #(x, x))] [x, y, ..xs] -> { let #(final, last) = impl([y, ..xs], x, x) [#(x, #(last, y)), ..final] } } }