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!
What platform are you using? If the platform is using Task
s then it will expect Tasks to be used everywhere and not purity inference.
Strange though... https://www.roc-lang.org/builtins/List#forEach! is included in the docs
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.
You may also be on an older nightly... so that could be an issue to.
Just throwing some ideas out there in case this helps. :smiley:
The problem of living between two worlds (tasks and purity inference).
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.
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
(the original snippet I posted above was me tooling around in the roc repl trying to find a solution lol)
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
That said, I think basic CLI expose Task.forEach
instead
Or, it's in the standard library
Has the same signature, so should just work instead of the list version
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?).
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