Hacker News new | past | comments | ask | show | jobs | submit login

The first item in the list is evaluated, just like the other items in the list. For example:

    ((LAMBDA (ARG1 ARG2 ARG3)
       <YOUR PROGRAM GOES HERE>)
     (QUOTE VAL1)
     (QUOTE VAL2)
     (QUOTE VAL3))
Is the same syntax as a normal function call:

    (PROGRAM
     (QUOTE VAL1)
     (QUOTE VAL2)
     (QUOTE VAL3))
The only difference is that, since it's the root-level program, the binding of the name PROGRAM can't have happened yet. So it's simply inlined into the expression. The Apply() part of the evaluator has a special case for recognizing this, which causes evaluation of the first argument to terminate:

    int Apply(int fn, int x, int a) {
      int t1, si, ax;
      if (ISATOM(fn)) {
        switch (fn) {
        case ATOM_CAR:
          return Car(Car(x));
        case ATOM_CDR:
          return Cdr(Car(x));
        case ATOM_ATOM:
          return ISATOM(Car(x)) ? ATOM_T : NIL;
        case ATOM_CONS:
          return Cons(Car(x), Car(Cdr(x)));
        case ATOM_EQ:
          return Car(x) == Car(Cdr(x)) ? ATOM_T : NIL;
        default:
          // recurse to turn (FUNC ARG1 ARG2) into ((LAMBDA ...) ARG1 ARG2)
          return Apply(Eval(fn, a), x, a);
        }
      }
      // evaluate ((LAMBDA ...) ARG1 ARG2)
      if (Car(fn) == ATOM_LAMBDA) {
        t1 = Cdr(fn);
        si = Pairlis(Car(t1), x, a);
        ax = Car(Cdr(t1));
        return Eval(ax, si);
      }
      return UNDEFINED;
    }
The Eval() function which calls Apply() has already evaluated all the list items except for the first one, which is left to Apply() to figure out. The reason why things are this way is because, in order to save space, the REPL loop doesn't maintain a persistent set of global variables. Due to the NIL here, each expression is its own hermetic job:

    void Repl(void) {
      for (;;) {
        Print(Eval(Read(), NIL));
      }
    }
If you changed that NIL to be an alist that's populated by things like defun / setq / etc. in the global scope, then the usability of the language improves dramatically. We didn't do it due to the singular focus on size. But at the same time that simply means we left fun opportunities for improvement to anyone wishing to hack on the codebase and make it their own.



Ok that makes sense. Thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: