diff --git a/Lab8/cs2030s/fp/InfiniteList.java b/Lab8/cs2030s/fp/InfiniteList.java index 67cd109..aabf282 100644 --- a/Lab8/cs2030s/fp/InfiniteList.java +++ b/Lab8/cs2030s/fp/InfiniteList.java @@ -72,7 +72,7 @@ public class InfiniteList { } public static InfiniteList iterate(T seed, Immutator func) { - Memo> head = Memo.from(() -> Actually.ok(seed)); + Memo> head = Memo.from(Actually.ok(seed)); Memo> tail = Memo.from(() -> iterate(func.invoke(seed), func)); return new InfiniteList<>(head, tail); } @@ -101,12 +101,16 @@ public class InfiniteList { if (n <= 0) { return InfiniteList.end(); } - if (this.isEnd()) { - return InfiniteList.end(); - } - Memo> head = Memo.from(() -> Actually.ok(this.head())); - Memo> tail = Memo.from(() -> this.tail().limit(n - 1)); + Memo> head = this.head; + + Memo> tail = Memo.from(() -> { + InfiniteList tempTail = this.tail.get(); + if (tempTail.head.get().unless(null) == null) { + return tempTail.limit(n); + } + return tempTail.limit(n - 1); + }); return new InfiniteList<>(head, tail); }