0
(define (pair a b)
    (lambda (f) (f a b)))

(define (fst x) .... )

'fst' is supposed to return a when i call (fst(pair 1 2))

I know that function (f a b) is stored in 'x' when i call (fst(pair 1 2)) How can I extract 'a' out? Any tips?

I tried using (car x) and (cdr x), but x is clearly not a list.

Edit: I ended up with this:

(define (fst x) (x (lambda (p q) p)))
(define (snd x) (x (lambda (p q) q)))

I am really new to scheme programming so i am still trying to understand how the compiler works. So, from this i learnt that if the "lambda" keyword is used after a function. so like (x (lambda (p q)), this actually extracts the p and q from x. Am i correct?

Thanks.

R J
  • 43
  • 1
  • 7
  • 1
    Hint: You are supposed to *implement* a list here. `pair` is `cons`, `fst` is `car`. Using `car` or `cdr` would be counter-productive, since the objective here is to learn how they can be implemented. Hint2: this is the standard Church Encoding of Pairs in λ-calculus. – Jörg W Mittag Sep 20 '15 at 23:58
  • @Jörg W Mittag what do you mean by "standard" Church Encoding. Does that mean, i can reduce it somehow? – R J Sep 21 '15 at 00:03
  • 1
    @RJ That's how you define functions like `cons`, `car`, `cdr`, ... in lambda calculus. Have a look at the *"Representing lists"* part : http://matt.might.net/articles/compiling-up-to-lambda-calculus/ – Kevin Sep 21 '15 at 00:11
  • I actually wrote this code for fun in a couple of languages a couple of years ago: http://joergwmittag.github.io/lambdaconscarcdr/ Currently, it still uses `if`, but I want to get rid of that, too, by using the Church Encoding of Booleans. I already have Ruby, Scheme, and ECMAScript versions of that, which I haven't committed yet. – Jörg W Mittag Sep 21 '15 at 08:34

0 Answers0