Stream: roctoberfest

Topic: Day 3


view this post on Zulip Ghislain (Oct 02 2022 at 20:24):

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:31):

@Ghislain there may have been a regression around this recently, but in theory you should be able to do something similar to Debug.log like this:

x = 5

expect x != x

blah

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:32):

when the expect x != xline runs (when you're doing roc dev), it should:

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:32):

if that's not working, let me know so we can file a bug about it!

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:41):

Shritesh Bhattarai said:

I encountered a lot of compiler crashes esp. when there was a type mismatch.

I wish I'd mentioned this sooner, but if you have time, it's extremely helpful to us to have reproductions of these - even if they aren't at all minimal! One trick I like to use is just do a quick git commit -m "reproduces compiler crash" before moving on, so it doesn't interrupt me too much, and then later on I can go back and file an issue for it with the actual reproduction (and then maybe someone else can shrink it down to a minimal reproduction)

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:42):

it's exciting to see all the feedback in this channel - thank you so much to everyone for sharing! :hug:

view this post on Zulip Ghislain (Oct 02 2022 at 20:43):

Ah, I didn't know it printed the value, that's great!
Though it seems that it does not work :cry:
Capture-décran-2022-10-02-à-22.40.19.png

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:43):

ah yeah I just realized https://github.com/roc-lang/roc/issues/4093 is still open

view this post on Zulip Richard Feldman (Oct 02 2022 at 20:44):

@Folkert de Vries if you have a chance, seems like a fix for https://github.com/roc-lang/roc/issues/4093 would immediately help people doing Roctoberfest! :smiley:

view this post on Zulip Shritesh Bhattarai (Oct 02 2022 at 22:09):

Day 3 part 1: https://github.com/shritesh/advent/blob/main/2021/3.roc
Bit-twiddling unboxed integers in a purely functional programming language: that's a first for me. I think we should have a clz (count leading zeros) builtin

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 22:10):

Yeah, we definitely should have count leading and trailing zeros. They will be needed for other things like dictionaries eventually.

view this post on Zulip Ayaz Hafiz (Oct 02 2022 at 22:57):

any objections to making a separate topic for each day? I know it might be a lot but probably easier to follow for posterity reasons than one big one here.

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 22:58):

Thats a good idea. I'll pull out these day 3 comments.

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 23:00):

Just finished day 3: https://github.com/bhansconnect/roc-aoc-2021/tree/trunk/day3

Definitely don't like this code very much. To be fair, I didn't go through the effort to deal with results, but I also didn't want them to just be a default value.

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 23:02):

I think handling results (especially ones you know should never error) can often get cumbersome. For my code, merging stages would help a lot, but I am intentionally trying writing in more chunked or pipelined way. Just isn't great in some cases.

view this post on Zulip Ayaz Hafiz (Oct 02 2022 at 23:05):

Yeah, I really want to reach for Rust's unreachable! or OCaml's failwithwhere I know invariants are not broken

view this post on Zulip Ghislain (Oct 02 2022 at 23:17):

Brendan Hansknecht said:

Just finished day 3: https://github.com/bhansconnect/roc-aoc-2021/tree/trunk/day3

Your shortcuts are mindblowing! :drooling:

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 23:20):

Just realized I forgot to push. part 2 up as well

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 23:21):

What do you mean by my shortcuts?

view this post on Zulip Ghislain (Oct 02 2022 at 23:23):

Your # Convert from utf8 to real numbers is so smart, my implementation takes so many lines for the same result

view this post on Zulip Ghislain (Oct 02 2022 at 23:25):

I learn so much from everyone here :tada: :innocent: thanks to this AoC

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 23:31):

Ah, yeah, normally I would write something like val - '0', but that doesn't directly work in Roc.

view this post on Zulip Shritesh Bhattarai (Oct 03 2022 at 00:22):

Part 2 is done: https://github.com/shritesh/advent/blob/main/2021/3.roc I was irked by bools not being in "the prelude" but then learned that tags are the better solution. Also, ranges should support a negative stepping IMO.

view this post on Zulip Ayaz Hafiz (Oct 03 2022 at 00:27):

Using a mask is a great idea. Removes a lot of error handling needed otherwise :sweat_smile:

view this post on Zulip Brendan Hansknecht (Oct 03 2022 at 00:29):

There was a good bit of discussion about List.range here: https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/List.2Erange.20boundaries

view this post on Zulip Brendan Hansknecht (Oct 03 2022 at 00:29):

Nothing concrete changed yet, but ranges will probably change in the future

view this post on Zulip Brendan Hansknecht (Oct 03 2022 at 00:30):

Also, good note about List.walkTry not being exposed. That definitely should change

view this post on Zulip Luke Boswell (Oct 08 2022 at 01:59):

I have finished Part 1 and thought I would share my code and some feedback Code.

It has taken me a 4-5 hours to get this far. I'm really happy with the result, and have found the experience to be really nice. The interactive workflow of working with the compiler and using "expect" tests helped me to learn as I went. I found a few compiler errors and minor issues along the way. As the tooling matures I see a lot of potential for this to be a very productive and maintainable language.

I tried a few different ways to get the answer and have taken then scenic route to get there, but my primary goal was to learn and explore the language. :sunglasses:

view this post on Zulip Brendan Hansknecht (Oct 08 2022 at 03:32):

Very interesting. I really like your solution. I feel like it is quite elegant!

view this post on Zulip Michał Łępicki (Oct 17 2022 at 22:03):

Day 3 done. Biggest struggles: when to Num.intCast (I'm kinda guessing for now), and how to "convince" the compiler that List.get will succeed when I know the length (I would prefer some way to assert that it does, and have some kind of panic at runtime, than writing code that does a default thing for the unreachable case)

view this post on Zulip Brendan Hansknecht (Oct 17 2022 at 22:19):

We may eventually get a userland crash function of some sort, until then, you can make your own. Give me one sec and I'll find the definition.

view this post on Zulip Brendan Hansknecht (Oct 17 2022 at 22:21):

getUnsafe : List a, Nat -> a
getUnsafe = \list, i ->
    when List.get list i is
        Ok x -> x
        Err _ -> crash {}

crash : {} -> *

view this post on Zulip Brendan Hansknecht (Oct 17 2022 at 22:21):

That should work

view this post on Zulip Brendan Hansknecht (Oct 17 2022 at 22:21):

Since crash is undefined, roc code will crash when it is called.

view this post on Zulip Michał Łępicki (Oct 18 2022 at 06:22):

That's convenient, I'll try it out - thanks!

view this post on Zulip Ghislain (Oct 18 2022 at 08:42):

Richard Feldman said:

Folkert de Vries if you have a chance, seems like a fix for https://github.com/roc-lang/roc/issues/4093 would immediately help people doing Roctoberfest! :smiley:

This issue fixes the expect inside a function running roc dev, but can roc test testing expect outside of a function (which works) does/could output the diff? (I'm on day 8 and I have to switch in JS to get debug info :man_facepalming: )

view this post on Zulip Folkert de Vries (Oct 18 2022 at 08:46):

it will output the values of variables

view this post on Zulip Folkert de Vries (Oct 18 2022 at 08:47):

so not quite a diff but at least better than "the expression is false"

view this post on Zulip Folkert de Vries (Oct 18 2022 at 08:47):

so

a = 4
b = 2
expect a == b

will tell you the values of a and b when the expect fails

view this post on Zulip Ghislain (Oct 18 2022 at 08:50):

That's exactly what I want, but it doesn't look to work, how do you make it work?

view this post on Zulip Ghislain (Oct 18 2022 at 08:51):

I run roc test and it just prints that the expectation failed

view this post on Zulip Folkert de Vries (Oct 18 2022 at 08:53):

ah, got the syntax slightly wrong

> cargo run -- test crates/cli_testing_examples/benchmarks/Scratchpad.roc
    Finished dev [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/roc test crates/cli_testing_examples/benchmarks/Scratchpad.roc`
── EXPECT FAILED ─────── crates/cli_testing_examples/benchmarks/Scratchpad.roc ─

This expectation failed:

 8│>  expect
 9│>      a = 1
10│>      b = 2
11│>
12│>      a == b

When it failed, these variables had these values:

a : Num a
a = 1

b : Num a
b = 2

1 failed and 0 passed in 504 ms.

produced by

expect
    a = 1
    b = 2

    a == b

view this post on Zulip Ghislain (Oct 18 2022 at 08:54):

Thank you!!

view this post on Zulip Ghislain (Oct 18 2022 at 08:57):

And it also prints the type :star_struck:

view this post on Zulip Austin Clements (Oct 26 2022 at 13:38):

Late to the day 3 party, but I was wondering if there's a way to specify a list/array of a specific length? I was thinking the same thing about using an unchecked List.get but it seems like it could be a risky assumption for code that changes over time and I was wondering if now or in the future there'd be a way to say "if I get to this point I know I have a list of exactly 12 numbers"

view this post on Zulip Brendan Hansknecht (Oct 26 2022 at 13:59):

I think the closest thing planned in roc is tuples. Since they have a compile time know size then you can pattern match on them to pull out a specific element.

The other possibility is to ignore the error case and cause the code to crash if that case happens. Roc may eventually have a first class crash, but currently it can implemented by writing s function with no body: crash : {} -> *

view this post on Zulip Brendan Hansknecht (Oct 26 2022 at 14:01):

Also, for creating a list of a specific length, you would probably use List.repeat defaultElement len

view this post on Zulip Austin Clements (Oct 26 2022 at 14:51):

Gotcha, makes sense and good tip about how to implement crash

view this post on Zulip Brendan Hansknecht (Oct 26 2022 at 16:04):

Oh, extra note: even though tuples don't exist yet, you can implement them with a single tag: T first second third.
Then pattern matching could be T _ second _ = myTuple

view this post on Zulip Austin Clements (Oct 26 2022 at 19:06):

Well here's my solution, maybe overkill with the types and helpers but I'm trying to learn some good error handling habits too, open to feedback :smile: https://github.com/austinclem1/roc-advent2021/blob/main/day03/main.roc


Last updated: Jul 06 2025 at 12:14 UTC