import java.lang.Boolean; sealed interface List permits LinkedElem, Elem {} public record LinkedElem(T a, List l) implements List {} public record Elem(T a) implements List {} public class PatternMatchingHaskellStyle { public append(LinkedElem(a, b), list2) { return handleAppend(a, b, list2); } private handleAppend(a, Elem(e), list2) { return new LinkedElem<>(a, new LinkedElem<>(e, list2)); } private handleAppend(a, LinkedElem(e,r), list2) { return new LinkedElem<>(a, append(new LinkedElem(e, r), list2)); } }