Oh, see also https://en.wikipedia.org/wiki/Mogensen%E2%80%93Scott_encodin... for a quick overview of Church and Scott encodings. (Ignore the stuff about Mogensen-Scott. Also, you'll need to have played around with lambda calculus a bit for this stuff to make sense. This kind of thing might be helpful: https://jacksongl.github.io/files/demo/lambda/index.htm)
To learn about the lambda calculus, check out chapter 5 of "Types and Programming Languages", Benjamin C. Pierce, MIT Press 2002. You might also get something out of "Semantics Engineering with PLT Redex", Felleisen, Findler & Flatt, MIT Press 2009.