Stream: beginners

Topic: learning


view this post on Zulip Benjamin Thomas (Feb 17 2024 at 11:59):

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

view this post on Zulip Richard Feldman (Feb 17 2024 at 11:59):

welcome! :wave: the unit type is {}

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:00):

I think you want Maybe a : [ ... ] instead of :=

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:00):

the way it's currently written, it's defining Maybe as an opaque type

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:01):

but the way it's being used is as a type alias (: instead of :=)

view this post on Zulip Benjamin Thomas (Feb 17 2024 at 12:06):

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

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:17):

ah, dbg can't be used as an expression yet, just a statement so far

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:19):

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

view this post on Zulip Richard Feldman (Feb 17 2024 at 12:19):

because it prints out the values of all named variables in the last expression

view this post on Zulip Benjamin Thomas (Feb 17 2024 at 12:23):

Many thanks that does it! :roc:

view this post on Zulip Oskar Hahn (Feb 17 2024 at 14:33):

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