Nix 101 Course / Section 2: The Nix language
Premium

2.8 Exercise: takeN and building lists recursively

Lesson 15 of 68 · 8:37

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

About this lesson

The second exercise turns the difficulty up a notch. You write takeN, a function that returns the first n items of a list, which means consuming one list while building another at the same time. If it takes you more than ten minutes, that is normal, and it means the exercise is doing its job.

In this video

  • The task, its two parameters, and the edge cases: the empty list and a list shorter than n
  • Why this one is harder than sum: the result list grows while the input list shrinks
  • A pause point to try it yourself before the walkthrough
  • Every recursion step written out by hand, substitution by substitution, until the answer appears
  • Why the recursive call needs no extra parentheses: function application binds stronger than operators

After this lesson

You can write recursive list functions on your own, which is all the language practice you need before writing expressions for real packages.

Links: