Stream: beginners

Topic: Sorting a list of str


view this post on Zulip Trevor Settles (Jan 23 2024 at 03:55):

I'm trying to do what it says on the tin. Str don't seem to implement the Eq ability, so its not that simple. I've gotten this so far:

compareStr : Str, Str -> [LT, EQ, GT]
compareStr = \a, b ->
    compareLetters (Str.toUtf8 a) (Str.toUtf8 b)

compareLetters : List u8, List u8 -> [LT, EQ, GT]
compareLetters = \x, y ->
    when (x, y) is
        ([], []) -> EQ
        ([], [_]) -> LT
        ([_], []) -> GT
        (a, b) ->
            if (List.first a) == List.frist b then
                ...
            else
                ...

My plan is to pass this into the List.sortWith function, but I'm finding it cumbersome to implement. My first thought was to write a recursive function that compares one letter at a time, but that doesn't seems easily possible.

Is there a pattern match that is something along the lines of [head, ..rest] -> ...

Otherwise, the List.first function, but this (understandably) returns Result a [ListWasEmpty]. Is there a way, similar to .unwrap() in Rust, that I can easily throw away the error, since I know what branch of the pattern match I'm on?

view this post on Zulip Luke Boswell (Jan 23 2024 at 04:03):

For pattern match the syntax is [head, .. as rest] -> ...

view this post on Zulip Luke Boswell (Jan 23 2024 at 04:06):

Richard added a detailed guide recently to https://www.roc-lang.org/builtins/Str which explains the logic behind not having Eq ability for Str

view this post on Zulip Luke Boswell (Jan 23 2024 at 04:06):

Refer to the section String equality and normalization

view this post on Zulip Brendan Hansknecht (Jan 23 2024 at 04:33):

Is this something the eventual unicode package plans to support?

view this post on Zulip Luke Boswell (Jan 23 2024 at 04:48):

I would guess so, but we need to implement the Unicode Normalization Algorithm I think

view this post on Zulip Richard Feldman (Jan 23 2024 at 05:07):

Luke Boswell said:

Refer to the section String equality and normalization

we should totally make these headers automatically self-linkifying like in the tutorial! :smiley:

view this post on Zulip Luke Boswell (Jan 23 2024 at 05:36):

I was thinking the same thing... should be another good first issue for Doc gen

view this post on Zulip Luke Boswell (Jan 23 2024 at 05:36):

Or maybe it can already be done

view this post on Zulip Luke Boswell (Jan 23 2024 at 05:40):

It's not implemented like the static-site-gen platform.

So we need to update docs gen crate to support e.g.

## ## [Syntax](#syntax) {#syntax} should generate

<h2 id="syntax"><a href="#syntax">Syntax</a></h2>

Sound ok?

view this post on Zulip Luke Boswell (Jan 23 2024 at 23:00):

I created an issue for this https://github.com/roc-lang/roc/issues/6420


Last updated: Jul 06 2025 at 12:14 UTC