Stream: beginners

Topic: patter matching for record


view this post on Zulip Artur Swiderski (Apr 16 2023 at 11:43):

I am completely lost in pattern matching for records
rec = { one: 1 two : 2 }
when rec is
{one _ , .. } -> # do something when one field is present in record
{} -> in any other record
Obviously it does not work this way, how to accomplish the task ?

view this post on Zulip Brian Carroll (Apr 16 2023 at 12:34):

Records are not what you want here. They do not have variable numbers of fields. Try using Dict, which has a function to get its size.

view this post on Zulip Brian Carroll (Apr 16 2023 at 13:00):

You won't be able to pattern match on Dict but you can get keys. Dict entries are unordered though. If you want them to be ordered then you need a List of key value pairs.

view this post on Zulip Anton (Apr 18 2023 at 09:54):

@Artur Swiderski we're trying to make an example for what you needed in this situation (with a Dict), is this example good?

view this post on Zulip Artur Swiderski (Apr 21 2023 at 16:37):

@Anton I already solved the problem but yes exactly what I wanted


Last updated: Jul 06 2025 at 12:14 UTC