Wizard
Wizard

Reputation: 22093

(caadr exp) in assignment-variable

I am reading 4.1.2 Representing Expressions of SICP,

which defines definition-variable.

  (define (definition? exp)
    (tagged-list? exp 'define))

  (define (definition-variable exp)
    (if (symbol? (cadr exp))
        (cadr exp)
        (caadr exp))) ;;

Refer to the conditionals

    (if (symbol? (cadr exp))
        (cadr exp)
        (caadr exp))) ;;

I am not very sure what (caadr exp) returned? is it a null or '()

The variable case

(define ⟨var⟩ ⟨value⟩)

(cadr exp) returns the second element as variable,

but alternative to procedure case:

  (define (⟨var⟩ ⟨param₁⟩ … ⟨paramₙ⟩)
    ⟨body⟩)

the expression (caadr exp))) is (car (car (cdr x)))

Since cadr is var, then caadr is a null?

Upvotes: 2

Views: 119

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

In the procedure case, (cadr exp) is not the name but a list whose first element is the name.

         (cadr exp)
             |
             v
         +-------+
         |       |
 (define (f a b c) body)  <--- exp
          ^
          |
      (caadr exp)

If you evaluate it:

(caadr '(define (⟨var⟩ ⟨param₁⟩ … ⟨paramₙ⟩) ⟨body⟩))
-->
(caar '((⟨var⟩ ⟨param₁⟩ … ⟨paramₙ⟩) ⟨body⟩))
-->
(car '(⟨var⟩ ⟨param₁⟩ … ⟨paramₙ⟩))
-->
⟨var⟩

Upvotes: 3

Related Questions