sealed interface List permits LinkedElem, Elem {} public record LinkedElem(T a, List l) implements List {} public record Elem(T a) implements List {} public class PaternMatchingHaskellStyle { public append(LinkedElem(a, Elem(e)), l2) { return new LinkedElem<>(a, new LinkedElem<>(e, l2)); } public append(LinkedElem(a, r), l2) { return new LinkedElem<>(a, append(r, l2)); } }