schemeracketchurch-encoding

Return something enclosed in parentheses in Scheme


I have the following piece of code for the successor and predecessor of Church numerals: Consider the following code:

(define zero (lambda () '() ))   ; Initialize Church numeral zero as nil
(define (succ x) (lambda () x))  ; Wraps x with another function and returns it
(define (pred x) (x))            ; "Unwraps" one shell function of x and returns it

(define one (succ zero))         ; gives me the Church numeral one
(pred one)

Suppose I do the following changes in the pred function:

(define (pred x) x)

What is the difference between returning x and (x)? What exactly does returning (x) mean syntactically and logically?


Solution

  • The function

    (define (pred x) x)
    

    is the identity function. The function accepts a value and binds it to the argument x. It then evaluates the body x which gives the original value back.

    The function

    (define (pred x) (x))
    

    takes one value as input and binds it the argument x. It then evaluates the body (x). The expression (x) means call (the hopefully a function) x with no arguments.

    (pred (lambda () 42)) will evaluate to 42.
    

    So in the context of your encoding (lamdda () x) wraps a function layer a value and (x) removes the function layer.