Stream: beginners

Topic: Access elements of Pair


view this post on Zulip Nils Hjelte (Sep 08 2024 at 15:38):

How do I access elements of Pair? Eg as used in the example of map2, zipped = List.map2 ["a", "b", "c"] [1, 2, 3] Pair. If I pipe this to List.all how do I unpack/destructure the elements of the pair? I tried \p -> p.0 == p.1 which seems to be tuple unpacking, and \p -> p.first == p.second, which tries to bind to a record with members first/second. (my example has integers in both lists, not string and int like the map2 example, so equality testing should be ok)

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:03):

So you used a tag as your map2 joining function

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:04):

So you have created Pair Str (Num a)

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:05):

So you would destructive the pair something like \Pair str int -> ...

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:07):

Also, you can destructive with a record or tuple as well:
\{first, second} -> ...
\(first, second) -> ...

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:08):

With the record, the names would need to match the field name

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:09):

For tags and tuples, the name can be anything

view this post on Zulip Brendan Hansknecht (Sep 08 2024 at 16:11):

And of course, instead of using Pair, you could just build a tuple or record:
List.map2 ["a", ...] [1, ...] \a, b -> (a, b)

view this post on Zulip Isaac Van Doren (Sep 08 2024 at 20:22):

If you use a tuple, you can access the fields with .0, .1, .2, etc:

("hello", "world").1 == "world"

List.map [("hello", "world")] .1 == ["world"]

Last updated: Jul 06 2025 at 12:14 UTC