Hello!
Could you tell me what I'm doing wrong here?
Maybe a := [Just a, Nothing]
last : List a -> Maybe a
last = \lst ->
when lst is
[] -> Nothing
[x] -> Just x
[_, .. as xs] -> last xs
Also, I'd like to know if there's a unit type? (I couldn't find it).
last : List a -> Result () a
last = \lst ->
when lst is
[] -> Err ()
[x] -> Ok x
[_, .. as xs] -> last xs
welcome! :wave: the unit type is {}
I think you want Maybe a : [ ... ]
instead of :=
the way it's currently written, it's defining Maybe
as an opaque type
but the way it's being used is as a type alias (:
instead of :=
)
Awesome thanks! I looked at Roc last year and I see it's evolving nicely, congrats :)
Is it possible to inspect a failed expect case. I'd like to see what's wrong when something failed.
I tried using dbg but it's not a valid construct
expect (dbg (last [1, 2, 3])) == Just 4
ah, dbg
can't be used as an expression yet, just a statement so far
but if you do this:
expect
actual = last [1, 2, 3]
actual == Just 4
then if it fails, roc test
will print out what actual
was
because it prints out the values of all named variables in the last expression
Many thanks that does it!
Roc does not use linked list but arrays/vectors. So you don't need a recursion.
last = \lst ->
when lst is
[] -> Nothing
[.., x] -> Just x
See: https://www.roc-lang.org/tutorial#pattern-matching-on-lists
Last updated: Jul 05 2025 at 12:14 UTC