Homework

Homework — a taste is enough

A short homework — one bite is enough for a taste:

  1. In the same experiment folder write sumTo(n int) int returning 1 + 2 + ... + n recursively. Write down for yourself: what is the base case? How does the task shrink?
  2. Next to it write sumToLoop — the same with for. Check that sumTo(100) and sumToLoop(100) both return 5050.
  3. In one sentence (a comment above sumTo) answer: which variant would you keep in a real program, and why?

Optional (not required — the final project will NOT need recursion): if you want a taste on your own data, write countUnread(items []Item, i int) int — counts unread books from index i: the base case i >= len(items) returns 0. Compare it with a range loop — and keep the loop.

To submit: the sumTo + sumToLoop code, both results for 100, and your one-sentence verdict.

Tip. You have had the taste — recursion returns in the Algorithms course, where it belongs. Now for the serious business. Your app already does everything... until you close it — every restart starts with an empty shelf. Next lesson your data finally survives a restart: we save to a file and load it back on startup. It is the last brick before the final project.