Stream: beginners

Topic: List append


view this post on Zulip Audrey Gao (Apr 05 2023 at 05:08):

Given that "List.append" will add an element to the end of a list, I am wondering is there a predefined function in the List Module that can append a list to the end of a list? Or a function that can insert an element in front of a list?

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 05:16):

List.concat
List.prepend

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 05:17):

We should probably add a perf note to List.prepend. It can cost a lot because it has to shift the entire list.

view this post on Zulip Audrey Gao (Apr 05 2023 at 05:19):

I didn't notice that there is a new documentation! Thanks a lot!

view this post on Zulip Audrey Gao (Apr 05 2023 at 05:37):

Actually I'm trying to build a self-defined map function for list. The following code encountered some error message that I couldn't figure out why.
image.png image.png

view this post on Zulip Audrey Gao (Apr 05 2023 at 05:39):

The code worked when I deleted the function type annotation.

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 05:50):

It would definitely be useful if you can file some bugs as you hit these. I think a lot of these are breaks that are likely to be web repl specific. With repros, we can slowly dig into them.

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 05:52):

My only comment on that code is I am surprised it parses. I would have expect that t1 = ... would be required to be on the next line, but that is clearly a different issue than the type bug you are hitting. My only guess with this bug is somehow the compiler running on a single thread with async and await breaks with this type checking.

view this post on Zulip Audrey Gao (Apr 05 2023 at 14:16):

If I understand correctly, the way I wrote the type checking itself is correct? I tried to run the same piece of code on LLVM backend in the terminal and it worked. But the roc repl with LLVM backend also failed to run this code, and it gives me the error: This Roc code crashed with: "a Lambda Set is empty. Most likely there is a type error in your program." I'm confused about the "type error in your program" thing, should I change the type annotation in some way to make it correct?

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 14:22):

Oh, actually, it may be incorrect. I think the extra parens around the record in the type should not be there

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 14:23):

Not sure if that is the bug. Specifically because I am unsure if we create single element tuples.

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 14:34):

Anyway, yeah, I am pretty sure this is a repl/compiler bug. Your example works in the command line repl if you enter it one definition at a time:

Enter an expression, or :help, or :q to quit.

» map : ({t0: I64 -> I64, t1: List I64}) -> List I64
… map = \{t0: f1, t1: lst} ->
…   when lst is
…     [] -> []
…     [hd, ..] ->
…       t1 = List.dropAt lst 0
…       List.concat [f1 hd] (map ({t0: f1, t1}))
…

<function> : { t0 : I64 -> I64, t1 : List I64 } -> List I64
                           # map

» f = \x -> x + 1
…

<function> : Num a -> Num a  # f

» map {t0: f, t1: [1,2,3]}
…

[2, 3, 4] : List I64      # val1

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 15:02):

Oh, interesting, this issue actually is reproducible in all locations, web repl, cmd repl, and real apps. It specifically is related to nesting somehow. The repl just automatically introduces the nesting so it is more likely to be found. Also, we should look at improving roc_panic in the web repl such that it prints out error messages.

view this post on Zulip Audrey Gao (Apr 05 2023 at 15:04):

Brendan Hansknecht said:

Oh, actually, it may be incorrect. I think the extra parens around the record in the type should not be there

Oh, deleting the extra parens around the record indeed fix the previous error! Thanks for the help!

view this post on Zulip Brendan Hansknecht (Apr 05 2023 at 15:04):

I also filed #5256 because there is definitely another issue here.


Last updated: Jul 05 2025 at 12:14 UTC