Explanation of the circuit (construction)

Can someone explain to me why:

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

(car (a)) ==> procedure

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

      

I don't think I understood. Thanks to

+3


source to share


2 answers


it

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

      

defines a procedure that a

, when called, returns a pair

(<the procedure a> . #f)

      

i.e. whose car

is the procedure itself and whose cdr

- #f

.

In other words, the evaluation result

(a)

      

is the result of calling a procedure a

with no arguments, which, as defined a

above,



(<the procedure a> . #f)

      

Hence,

(car (a))

      

is <the procedure a>

(because it means "call car

with the result of the evaluation (a)

")

When you add another pair of parentheses

((car (a)))

      

you call this procedure, which - since it is a procedure a

- returns the same result as (a)

,

 (<the procedure a> . #f)

      

+2


source


define

from the top level defines a global variable a

.

(lambda() (cons a #f)

When called, an anonymous procedure makes a pair of evaluating a

and #f

.

When you rate a

, you get the procedure. On my system, you get #<procedure:a>

.



When you rate (a)

, you receive (#<procedure:a> . #f)

. Now the way procedures are displayed is highly implementation dependent. There are no standards, but many will use a convention with a name a

, but don't count on it.

Since a

you can also get as the car

result of a call a

, you can ((car (a)))

and get the same thing as (a)

. This is because (eq? a (car (a)))

#t

.

+1


source







All Articles