P-Lisp Notes ------------(02-Jul-2008) Intrinsic functions (from manual and experiment) ------------------------------------------------ (CAR A) Return the first element of the list A (CDR A) Return everything but the first element of the list A. If A is an atom, return a list of its properties. (CHR 65) Return an atom that is named the ASCII character whose code is given. Use (CHR 4) to do DOS 3.3 commands. (GREATER A B) Return T (true) if A > B. With this and EQUAL all comparisons can be done. For example, A < B is (GREATER B A). (ADD A B) Add A and B and return the result. (MULT A B) Multiply A by B. (DIV A B) Divide A by B. (SUB A B) Subtract B from A. (NUMBER A) Return T if A is a number, NIL otherwise. (ATOM A) Return T if A is an atom, NIL otherwise. (ZERO N) Return T if N is zero. (CONS A B) Make a pair. If A is an atom and B is a list, put A at the head of the list. (CONC A B) Concatenate list B to the end of list A. Both must be lists. (SETQ NAME VALUE) Associate a value with a name. "AN ATOM WITH SPACES" These can be used as strings, after a fashion. They still must be quoted (') though. (NULL (EQUAL A B)) (NOT (EQUAL A B)) Logical not, test for NIL. (LIST A B ...) Make a list out of the arguments. (QUOTE A) | 'A Quote a list or atom (prevent it from being evaluated). (TRACE FUNC) (UNTRACE FUNC) Use to trace the execution of a function. (MAPCAR 'PRINT '(HOW NOW BROWN COW)) Map a function over a list. (INT N) Return the integer part of a number (floor). (LENGTH L) Return the number of elements in a list (top level). (EXPLODE A) Return the atom A as a list with each character of the name a separate element: (EXPLODE 'XYZZY) => (X Y Z Z Y) (IMPLODE A) Inverse of EXPLODE. (IMPLODE '(X Y Z Z Y)) => XYZZY (AND A B ...) Logical AND of all the arguments. Lazy evaluation. (OR A B ...) Logical OR of all the arguments. Lazy evaluation. (EVAL EXPR) Evaluate an expression. Use (EVAL (READ)) to execute what a user types. (APPLY FUNC ARGS) Apply a function (FUNC) to a list of its arguments (ARGS). If FACT takes one argument, then (APPLY 'FACT '(5)) is (FACT 5), etc. (PUT A PROP VAL) Put a property into an atom, (PUT 'A 'NAME 'BIGGLES). (GET A PROP) Get a property value, (GET 'A 'NAME) => BIGGLES. (REM A PROP) Remove a property, (REM 'A 'NAME). (READ) Read a Lisp expression (S-exp) from the user and return it unevaluated. (PRINT A) (PRIN1 A) Print A (with a newline). Use PRIN1 to suppress the newline. (GC) | (GC NIL) Force a garbage collection. Returns the number of free cells. If NIL given, do not print a garbage collection message. ; Apple II specific <-- ; starts a comment (GR) (COLOR C) (PLOT X Y) Low res graphics. (HGR2) (HCOLOR C) (HPLOT X Y) High res graphics. Page 2. (TEXT) (HTAB N) (VTAB N) Set text mode, position cursor for printing. (PEEK A) Return the value stored in memory location A. The address must be a signed 16-bit int, -32768..32767. (POKE A N) Set the value of memory location A to N. (CALL A) Call a machine language routine, eg, (CALL -936) == HOME. Use (CALL -151) to get to the monitor, ctrl-Y returns to Lisp. Predefined Constants -------------------- T true NIL false Control Structures ------------------ (COND (P1 E1) (P2 E2) ... ) Standard Lisp conditional statement. Evaluate P expressions until one of them is true, then do the E part returning its value. Typically, the last P expression is just T, to do it not matter what. With recursion this is powerful. (PROG (A B ...) LOOP (FUNC1 ...) (FUNC2 ...) (GO LOOP) (RETURN VALUE) ) Set up a local environment with local variables A, B, etc. Then evaluate expressions sequentially. Ignore lone atoms (like LOOP) as these are merely labels. Return the VALUE of the return statement and use GO as a goto. For non-recursive iteration, which is necessary on a small system like the Apple II with limited memory. Not tail-recursive like Scheme. (PROGN (FUNC1 ...) (FUNC2 ...) (FUNC3 ...) ) A simplified PROG construct. Evaluate expressions sequentially and return the value of the last one. Function definition ------------------- (DEFINE (FUNC (LAMBDA (A B ...) ...BODY... ) For example, a recursive factorial: (DEFINE (FACT (LAMBDA (N) (COND ((EQUAL N 1) 1) (T (MULT N (FACT (SUB N 1)))))))) Save/Load Environment --------------------- (SAVE FILENAME) (LOAD FILENAME) Editor ------ The built-in editor is too primitive. I'm using LED instead. Run LED.HELP to see the commands.