On 2016-10-24, Albert van der Horst <
alb...@cherry.spenarnc.xs4all.nl> wrote:
> For list please read expression or whatever the correct wording
> would be.
>
> There is the famous READ-EVALUATE-PRINT loop.
> In implementing a lisp, I've thought to leave EVALUATE
> out at first.
> Is that feasible?
> That means : does READ leave a Lisp list that can be
> PRINTed as a test?
Of course; and you can always get at it by wrapping it in QUOTE
to suppress evaluation.
> Or does EVALUATE in fact just transform a list into an other list
It's called eval in pretty much every mainstream dialect of Lisp.
> in the same internal representation?
No; some compound expressions produce non-lists, and non-lists
are also expressions that can be input to eval.
In anything that can be called a real Lisp, the input to eval,
if it is a compound expression, is a list: the same stuff
that is produced by eval, if the expression is of a kind
that computes a list.
> Based on McCarthy's original microlisp document, I've implemented
> the elementary commands up till lambda. It took about a day.
> It seems that document is much better than many books that
> are written about lisp.
>
> I've discovered something. I can have a PLUS function and
> a PLUS symbol that is bound to a number at the same time.
> (DEFUN PLUS ( A B ) (+ A B ) )
> (SETQ PLUS 117)
> So LISP has (at least) two namespaces.
> Why is that kept in the dark?
Could be due to a prerence for tiny, six-decade-old documents over
books.
> As a sequel I tried to do the following
> (defun PLUS ( A B ) (+ A B ) )
> (PLUS 12 3 )
> 15
> That obviously works.
> Now I tried to find an expression <e> such that
> (<e> 12 3)
> where <e> evaluates to PLUS, and then the total evaluates to 15.
> Nothing worked.
It's your code!
Does your interpreter evaluate an expression occurring
in the position indicated by <e>?
A traditional two-namespace Lisp (a "Lisp-2" dialect) won't evaluate
<e>. If yours works like that, you're wasting time trying various
expressions there; none will be evaluated.
> Is there a fundamental reason that can't be done, or am
> I just ignorant?
You must implement a function traditionally called FUNCALL, so
you can then do:
(FUNCALL <e> 12 3)
FUNCALL should typically accept, as its first argument, either a
function object (to which it just applies the arguments directly),
or else a symbol (which it resolves to a function object by a lookup
in the global function binding environment, and then applies the
arguments).
Notable "Lisp-1" dialects in which funcall isn't necessary are Scheme
and EuLisp.
ANSI Common Lisp is a Lisp-2 with funcall.
In Common Lisp, by the way, you can still do this:
((lambda (...) ...) arg1 arg2 ...)
A lambda expression in the first position of a compound form is
recognized as denoting a function. This is because it serves
as a function name.
It works in other argument positions also, but only thanks
to a macro called lambda, which transforms:
(lambda (...) ...) -> (function (lambda (...) ...))
The special operator FUNCTION recognizes a lambda expression
as a function name and produces the object that it denote.
In Lisp-1 dialects things are a bit simpler: lambda is usually just an
operator which is evaluated, and calculates a function as it result.