roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.18k stars 297 forks source link

Improve user experience for big `List.range` calls #7032

Open Anton-4 opened 1 month ago

Anton-4 commented 1 month ago

Relocated from https://github.com/roc-lang/examples/issues/199. issue reported by @alexpyattaev:

The example below segfaults on roc --version: roc nightly pre-release, built from commit faeea528 on Fr 23 Aug 2024 09:01:42 UTC There are no compilation errors of any kind.

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.14.0/dC5ceT962N_4jmoyoffVdphJ_4GlW3YMhAPyGPr-nU0.tar.br" }

import pf.Stdout
import pf.Task

loop = \i, max ->
    if i < max then
        i + loop (i + 1) max
    else
        i

main =
    x = 1000 * 1000 * 1000
    z = loop 0 x
    # z = List.range {start: At 0, end: Before x} |> List.walk 0 Num.add
    Stdout.line! "Hello, World! $(Num.toStr z)"

The commented out line was the other attempt at computing the same sum, which resulted in OOM kill of the process as it tried to allocate 1 billion element long List. Not sure why, but it was clearly unnecessary here.

changing the loop impl to this makes the thing work correctly, but it is ugly as sin and not obvious for the user to figure out what the problem is.

loop = \sum, i, max ->
    if i < max then
        loop (sum + i) (i + 1) max
    else
        sum
Anton-4 commented 1 month ago

Discussion of issue