I am referring to this question
type Churchlist t u = (t->u->u)->u->u
In lambda calculus, lists are encoded as following:
[] := λc. λn. n
[1,2,3] := λc. λn. c 1 (c 2 (c 3 n))
mapChurch :: (t->s) -> (Churchlist t u) -> (Churchlist s u)
mapChurch f l = \c n -> l (c.f) n
I am thinking about what other list functions I could implement on Churchlists and successfully wrote a conc2 function that concatenates 2 church lists
conc2Church l1 l2 c n = l1 c (l2 c n)
I also tried a zipWithChurch that operates like zipWith on normal lists. But i can not find a solution. Can anyone help me?
Do you want to use real tuples, or church tuples? I'll assume the former.
So, start with the desired type signature. You want it to take in 2 different
Churchlist
s and produce aChurchlist
of tuples.Now how would you implement this? Recall that
Churchlist
s are represented by the function that folds over them. So if our result is aChurchlist (a,b) u
, we'll want it to have the form of a function of the type((a,b) -> u -> u) -> u -> u
(this is, after all, the equivalent to the type synonymChurchlist (a,b) u
).What is the next step? Well, that depends. Is
l1
empty? What aboutl2
? If either of them are, then you want the result to be the empty list. Otherwise, you want to pair up the first elements from each list, and then churchZip the rest.This brings up some questions.
churchHead
,churchTail
, andisEmpty
available? Are you willing to write them? Can you write them?l1
andl2
actually are the folding function over themselves), but is that a clean solution to this problem?Getting up to this point is purely mechanical, assuming a firm understanding of the church encoding of lists. I'll leave the deep thinking up to you, since this is homework.