Search code examples
schemeelementcons

Scheme explanation(construct)


Can someone please explain me why :

(define a (lambda() (cons a #f)))

(car (a)) ==> procedure

((car (a))) ==> (procedure . #f)

I don't think I get it. Thanks


Solution

  • This

    (define a (lambda() (cons a #f)))
    

    defines a procedure, a, which when called will return the pair

    (<the procedure a> . #f)
    

    i.e. whose car is the procedure itself, and whose cdr is #f.

    In other words, the result of evaluating

    (a)
    

    is the result of calling the procedure a with no arguments, which is, by definition of a above,

    (<the procedure a> . #f)
    

    Hence,

    (car (a))
    

    is <the procedure a> (because it means "call car with the result of evaluating (a)")

    When you add another pair of parentheses

    ((car (a)))
    

    you're calling that procedure, which - since it's the procedure a - returns the same result as (a),

     (<the procedure a> . #f)