# Understanding the UNPAIR syntactic sugar

UNP(\left=A|P(\left)(\right))(\right=I|P(\left)(\right))R: A syntactic sugar for destructing nested pairs. These macros follow the same convention as the previous one.
> UNPAIR / S => DUP ; CAR ; DIP { CDR } / S
> UNPA(\right)R / S => UNPAIR ; DIP (UN(\right)R) / S
> UNP(\left)IR / S => UNPAIR ; UN(\left)R / S
> UNP(\left)(\right)R => UNPAIR ; UN(\left)R ; UN(\right)R / S


When I read this, sometimes UNP(\left)IR is used, other times UN(\left)R is used. I'm not sure how to interpret an UNP(\left)(\right)R. If an example like

P A  P P A  I    I R
( l, ( ( l, r ), r ))


was provided I think it would be much clearer

As a user, you can understand it in exactly the same way as the P*R macro. You only need to decode the \left/\right notation there if you want to understand the implementation.

Here is an example:

UNIT :d; UNIT :c; UNIT :b; UNIT :a;
PAPPAIIR; # (pair (unit :a) (pair (pair (unit :b) (unit :c)) (unit :d))) : S
UNPAPPAIIR; # (unit :a) : (unit :b) : (unit :c) : (unit :d) : S


The pair structure associated with PAPPAIIR and UNPAPPAIIR is the same, (a, ((b, c), d)). UNPAPPAIIR just operates in the reverse direction.

To understand the implementation/specification there, you need to read \left and \right as variables, bound on the left of => and substituted into the right of =>.

• makes sense, although I think there's a typo with UNP(\left)IR and UN(\left)R both being used – Rob Feb 13 '19 at 23:07
• That is not a typo. However, the spec does appear to have some mistakes. And in any case, it does not match the real implementation. – Tom Feb 13 '19 at 23:10