2024-10-25 09:12:46 +00:00
|
|
|
sealed interface List<T> permits LinkedElem, Elem {}
|
|
|
|
|
|
|
|
public record LinkedElem<T>(T a, List<T> l) implements List<T> {}
|
2024-10-25 09:10:00 +00:00
|
|
|
public record Elem<T>(T a) implements List<T> {}
|
|
|
|
|
|
|
|
public class PaternMatchingHaskellStyle {
|
|
|
|
public append(LinkedElem(a, Elem(e)), l2) {
|
|
|
|
return new LinkedElem<>(a, new LinkedElem<>(e, l2));
|
|
|
|
}
|
|
|
|
|
|
|
|
public append(LinkedElem(a, r), l2) {
|
2024-10-25 10:06:23 +00:00
|
|
|
return new LinkedElem<>(a, append(r, l2));
|
2024-10-25 09:10:00 +00:00
|
|
|
}
|
|
|
|
}
|