this post was submitted on 02 Mar 2024
13 points (93.3% liked)

Nix / NixOS

1779 readers
1 users here now

Main links

Videos

founded 1 year ago
MODERATORS
 

Hey, i currently try to learn the nix language and i have a question about recursion. This is my solution for Advent of Code 2015 day 4

let
    input = "abcdef";
    part1 = number:
        if (builtins.substring 0 5 (builtins.hashString "md5" "${input}${builtins.toString number}")) == "00000" then
            number
        else
            part1 (number + 1);
    part2 = "not implemented";

in {"Part 1" = part1 1; "Part 2" = part2;}

since i put the recursive call at the end of the function i assumed it would do some magic or tail call optimization stuff, but it results pretty fast in a stack overflow is there a way to get it working?

top 1 comments
sorted by: hot top controversial new old
[–] [email protected] 5 points 8 months ago

Nix' stack size is quite limited for a functional language. You cannot have infinite lists either. IIRC it does not do any tail call optimisation; it's a simple recursive evaluation.

Note that Nix is not a general purpose programming language that is designed to solve general purpose problems such as this one.