Search code examples
schemelispsicp

If and cond as special forms


Consider these two paragraphs from SICP:

This construct is called a case analysis, and there is a special form in Lisp for notating such a case analysis. It is called cond (which stands for “conditional”), and it is used as follows:

...

This uses the special form if, a restricted type of conditional that can be used when there are precisely two cases in the case analysis.

What does type mean in this context (restricted type of conditional)? Does it mean:

  • "if" is a type of "cond"? Because the sentence states "there is a special form", so there is only one special form because "if" is one type of "cond".
  • Both "if" and "cond" are unrelated. They are both conditionals. If this is correct, why does this sentence say "there is a special form" like it is only one?

Solution

  • In " if [is] a restricted type of conditional", I believe "conditional" doesn't mean specifically cond; it means "conditional statement / expression", in general.

    So there are two, cond and if. Each can be defined in terms of the other, so a given implementation may choose to have only one of them as a primitive, defining the other in terms of it; or the implementation can choose to have both of them as primitive special forms.

    Special forms are handled specially by the interpreter (compiler) itself.

    Macros also can be used for that. They won't be handled by the interpreter itself then, but rather by its macros-handling mechanism.

    So if is a conditional; cond is a conditional; cond can have any number of clauses; if must have exactly two (or one or two, depending on the standard) clauses; all the rest is just English. :)