Stream: beginners

Topic: Exclamation mark for List.forEach! not being detected??


view this post on Zulip pci_express (Nov 30 2024 at 21:21):

Hey all! I'm very new to Roc, and I just ran into what seems to be a very basic error, but I can't find an obvious answer to:

The List module does not expose `forEach`:

4│      List.forEach! [1, 2, 3] \num -> "Hi"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Did you mean one of these?

    List.forEach!
    List.forEachTry!
    List.concat
    List.first

I've been getting the above List.forEach! error no matter what I do or where I run it (command line, web repl). The compiler just doesn't seem to detect the exclamation mark in my code? This is not a universal problem, because I'm able to use other functions like Stdout.line! just fine. Am I missing something obvious here? I'm running with the latest Roc nightly build 2024-11-29-d72da8e for linux x86-64. Thanks in advance!

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:23):

What platform are you using? If the platform is using Tasks then it will expect Tasks to be used everywhere and not purity inference.

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:23):

Strange though... https://www.roc-lang.org/builtins/List#forEach! is included in the docs

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:25):

Are you able to post a larger snippet of your code? The error shows you are returning an expression, the string literal "Hi" which wouldn't be an effectful function call.

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:26):

You may also be on an older nightly... so that could be an issue to.

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:26):

Just throwing some ideas out there in case this helps. :smiley:

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:32):

The problem of living between two worlds (tasks and purity inference).

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:34):

In your example above, I think you would just want List.map. If you do need effects, you probably have to use a recursive function or List.walk to build up a task. List.forEach! only works with purity inference as luke mentioned above.

view this post on Zulip pci_express (Nov 30 2024 at 21:36):

My platform is https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br.

Here's the full main.roc file I've got right now (excluding testing code I wrote further down):

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br" }
import pf.Stdout
import "test.txt" as sample : List U8

main =
    Stdout.line! "I'm a Roc application!"
    total = addAndStringify {birds: 3, iguanas: 2}
    Stdout.line! "There are $(total) animals"
    List.forEach! ["entry1", "entry2", "entry3"] \str ->
        Stdout.line! str

# New helper function!
addAndStringify = \count ->
    sum = count.birds + count.iguanas
    if sum == 0 then
        ""
    else if sum < 0 then
        "negative"
    else
        Num.toStr sum

view this post on Zulip pci_express (Nov 30 2024 at 21:36):

(the original snippet I posted above was me tooling around in the roc repl trying to find a solution lol)

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:39):

Yeah, basic CLI does not yet have a release with purity inference (PR is so close to landing). So it can't use anything in the standard library with a ! in the name

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:39):

That said, I think basic CLI expose Task.forEach instead

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:40):

Or, it's in the standard library

view this post on Zulip Brendan Hansknecht (Nov 30 2024 at 21:41):

Has the same signature, so should just work instead of the list version

view this post on Zulip pci_express (Nov 30 2024 at 21:41):

ah ok cool, thanks for the tips @Luke Boswell @Brendan Hansknecht !! Another noob question: what are tasks/purity inference (and/or is there a link where I can read more about them )? For context, my programming background is all in Rust/C++/C systems land, so I haven't substantially interacted with functional languages prior (if this is a functional languages property?).

view this post on Zulip Luke Boswell (Nov 30 2024 at 21:51):

Richard has a talk on it https://youtu.be/42TUAKhzlRI?si=K_MSSRqBgngcvJb0 "The Functional Purity Inference Plan"


Last updated: Jul 06 2025 at 12:14 UTC