Hey Y'all, I'm just getting started with Roc and thought that implementing some classic sorting algorithms would be an interesting exercise.
Here's my code: https://gist.github.com/mulias/c8920302f5bb63dbc922e8914420dfc8
slowQuicksort
is working fine, it partitions the list into sublists then joins them back up recursively.
fastQuicksort
(quickersort?) passing around the full list and operates on sublists by index, under the theory that the compiler will optimize theList.swap
s and get similar performance to an imperative solution.
In practice I can't get the code to compile on my machine. It seems to hang and eats up a bunch of memory. If I comment out the fastQuicksort
code it works again.
How do I start debugging this kind of issue?
I think you hit a compiler bug, not sure how exactly, but it seems related to the List.walkFromUntil
in partition.
At least that gives us a relatively small repro
So definitely a roc bug that we don't give you an error and just hang, but the root cause of the issue is the function passed to List.walkFromUntil
.
The value returned from Break
needs to match the value used in Continue
they both have the same type variable, namely state
This is because Break
is not guaranteed to get hit. If you use of the entire list and never hit a Break
, the last state from Continue
will be returned.
So, not saying this is correct at all, but it compiles:
(outI, _, outList) =
List.walkFromUntil list start (firstPivotIndex, firstIndex, list) \(pivotIndex, index, listAcc), elem ->
if index >= end then
finalPivotIndex = pivotIndex - 1
Break (firstPivotIndex, index, List.swap listAcc start firstPivotIndex)
else if compare elem pivot == LT then
Continue (pivotIndex, index, listAcc)
else
Continue (pivotIndex + 1, index + 1, List.swap listAcc pivotIndex index)
(outI, outList)
Hopefully that unblocks you.
I'll file an issue for the hang since it is easy to repro.
filed #5595
That makes sense, thank you! I'm also not confident it's correct, but being able to compile will definitely help.
Last updated: Jul 06 2025 at 12:14 UTC