2024-10-25 12:04:03 +00:00
|
|
|
import java.lang.Boolean;
|
|
|
|
|
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> {}
|
|
|
|
|
2024-11-07 16:04:43 +00:00
|
|
|
public class PatternMatchingHaskellStyle {
|
2024-10-25 12:04:03 +00:00
|
|
|
|
|
|
|
public append(LinkedElem(a, b), list2) {
|
2024-10-25 13:31:48 +00:00
|
|
|
return handleAppend(a, b, list2);
|
2024-10-25 12:04:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 13:31:48 +00:00
|
|
|
private handleAppend(a, Elem(e), list2) {
|
2024-10-25 13:58:18 +00:00
|
|
|
return new LinkedElem<>(a, new LinkedElem<>(e, list2));
|
2024-10-25 12:04:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 13:31:48 +00:00
|
|
|
private handleAppend(a, LinkedElem(e,r), list2) {
|
2024-10-25 13:58:18 +00:00
|
|
|
return new LinkedElem<>(a, append(new LinkedElem(e, r), list2));
|
2024-10-25 12:04:03 +00:00
|
|
|
}
|
|
|
|
}
|