Nix 101 Course / Section 2: The Nix language
Premium

2.7 Exercise: sum a list with recursion

Lesson 14 of 68 · 5:42

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

About this lesson

Time to write your first recursive Nix function yourself. The task: a sum function that takes a list of numbers and returns their total. No loops, no mutation, just recursion. It sounds small, and that is the point: this is the pattern behind real Nix code everywhere.

In this video

  • The exact task and test cases, including what sum of an empty list should return
  • The only hints you need: head, tail, and checking for the empty list first
  • A pause point to solve it on your own before the walkthrough begins
  • A trace added to the finished function so you can watch the numbers add up, call by call

After this lesson

Once you have written this function with your own hands, the recursion pattern stops being theory and becomes something your fingers know.

Links: