Homework — a taste is enough
A short homework — one bite is enough for a taste:
- In the same experiment folder write
sumTo(n int) intreturning1 + 2 + ... + nrecursively. Write down for yourself: what is the base case? How does the task shrink? - Next to it write
sumToLoop— the same withfor. Check thatsumTo(100)andsumToLoop(100)both return5050. - 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.