when
, I had to write 49
(even though ['1'] == Str.toScalars "1"
in repl
). I get a mismatch Builtin(Int(I32)) == Builtin(Int(U32))
expect
but I miss my loving print/console
debugger :innocent: (also would love a fix of multilines in repl
https://github.com/roc-lang/roc/issues/2666)Str.concat
, like a Str.withPrefix
? ("01" |> Str.xxx "0b" == "0b01"
)@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
when the expect x != x
line runs (when you're doing roc dev
), it should:
x
wasif that's not working, let me know so we can file a bug about it!
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)
it's exciting to see all the feedback in this channel - thank you so much to everyone for sharing! :hug:
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
ah yeah I just realized https://github.com/roc-lang/roc/issues/4093 is still open
@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:
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
Yeah, we definitely should have count leading and trailing zeros. They will be needed for other things like dictionaries eventually.
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.
Thats a good idea. I'll pull out these day 3 comments.
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.
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.
Yeah, I really want to reach for Rust's unreachable!
or OCaml's failwith
where I know invariants are not broken
Brendan Hansknecht said:
Just finished day 3: https://github.com/bhansconnect/roc-aoc-2021/tree/trunk/day3
Your shortcuts are mindblowing! :drooling:
Just realized I forgot to push. part 2 up as well
What do you mean by my shortcuts?
Your # Convert from utf8 to real numbers
is so smart, my implementation takes so many lines for the same result
I learn so much from everyone here :tada: :innocent: thanks to this AoC
Ah, yeah, normally I would write something like val - '0'
, but that doesn't directly work in Roc.
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.
Using a mask is a great idea. Removes a lot of error handling needed otherwise :sweat_smile:
There was a good bit of discussion about List.range here: https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/List.2Erange.20boundaries
Nothing concrete changed yet, but ranges will probably change in the future
Also, good note about List.walkTry not being exposed. That definitely should change
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:
Very interesting. I really like your solution. I feel like it is quite elegant!
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)
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.
getUnsafe : List a, Nat -> a
getUnsafe = \list, i ->
when List.get list i is
Ok x -> x
Err _ -> crash {}
crash : {} -> *
That should work
Since crash is undefined, roc code will crash when it is called.
That's convenient, I'll try it out - thanks!
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: )
it will output the values of variables
so not quite a diff but at least better than "the expression is false"
so
a = 4
b = 2
expect a == b
will tell you the values of a and b when the expect fails
That's exactly what I want, but it doesn't look to work, how do you make it work?
I run roc test
and it just prints that the expectation failed
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
Thank you!!
And it also prints the type :star_struck:
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"
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 : {} -> *
Also, for creating a list of a specific length, you would probably use List.repeat defaultElement len
Gotcha, makes sense and good tip about how to implement crash
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
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