Nix 101 Course / Section 2: The Nix language
Premium

2.6 Lists and recursion in Nix

Lesson 13 of 68 · 13:22

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

About this lesson

Nix has no for loops, so lists and recursion do the work that loops do elsewhere. This lesson shows how to take lists apart with head and tail, glue them back together with ++, and set up a recursive function in a language where every file is one single expression.

In this video

  • builtins.length, head and tail, and the exception waiting for you on the empty list
  • Two syntax pitfalls: operator precedence around ++, and why x-1 is a variable name, not a subtraction
  • A Fibonacci function, and the let in trick that lets a function call itself by name
  • Walking a list recursively with trace output, and the lazy evaluation detail that keeps it from crashing

After this lesson

You can decompose, rebuild and recurse over lists, which is exactly what the next two exercises assume you can do.