C

*Class
A global variable holding the current class. See also OO Concepts, class, extend, dm and var and rel.


: (class +Test)
-> +Test
: *Class
-> +Test
(caaar 'lst) -> any
List access shortcut: Equivalent to (car (car (car 'lst))).


: (caaar '(((1 2) 3) 4))
-> 1
(caadr 'lst) -> any
List access shortcut: Equivalent to (car (car (cdr 'lst))).


: (caadr '(1 (2 3)))
-> 2
(caar 'lst) -> any
List access shortcut: Equivalent to (car (car 'lst)).


: (caar '((1 2) (3 4)))
-> 1
(cache 'var 'sym . prg) -> any
Speeds up some calculations, by holding previously calculated results in an idx tree structure. sym must be a transient symbol representing a unique key for the argument(s) to the calculation.


: (de fibonacci (N)
   (cache '*Fibonacci (format N)
      (if (< N 2)
         1
         (+
            (fibonacci (dec N))
            (fibonacci (- N 2)) ) ) ) )
-> fibonacci
: (fibonacci 22)
-> 28657
: (fibonacci 10000)
-> 5443837311356528133873426099375038013538 ...  # (2090 digits)
(cadar 'lst) -> any
List access shortcut: Equivalent to (car (cdr (car 'lst))).


: (cadar '((1 2 3)))
-> 2
(cadddr 'lst) -> any
List access shortcut: Equivalent to (car (cdr (cdr (cdr 'lst)))), or the fourth element of lst.


: (cadddr (1 2 3 4 5 6))
-> 4
(caddr 'lst) -> any
List access shortcut: Equivalent to (car (cdr (cdr 'lst))), or the third element of lst.


: (caddr (1 2 3 4 5 6))
-> 3
(cadr 'lst) -> any
List access shortcut: Equivalent to (car (cdr 'lst)), or the second element of lst.


: (cadr (1 2 3 4 5 6))
-> 2
(call 'any ..) -> flg
Calls an external system command. The any arguments specify the command and its arguments. Returns T if the command was executed successfully.


: (when (call 'test "-r" "file.l")  # Test if file exists and is readable
   (load "file.l")                  # Load it
   (call 'rm "file.l") )            # Remove it
(car 'lst) -> any
List access: Returns the first element of lst.


: (car (1 2 3 4 5 6))
-> 1
(case 'any (any1 . prg1) (any2 . prg2) ..) -> any
Multi-way branch: any is evaluated and compared to the CAR elements anyN of each clause. If one of them is a list, any is in turn compared to all elements of that list. T is a catch-all for any value. If a comparison succeeds, prgN is executed, and the result returned. Otherwise NIL is returned.


: (case (char 66) ("A" (+ 1 2 3)) (("B" "C") "Bambi") ("D" (* 1 2 3)))
-> "Bambi"
(catch 'sym . prg) -> any
Sets up the environment for a non-local jump with throw. sym is used by throw as a jump label (with T being a catch-all for any label). If throw is called during the execution of prg, the value thrown is returned immediately. Otherwise, the result of prg is returned. See also finally.


: (catch 'Ok (println 1) (throw 'Ok 999) (println 2))
1
-> 999
(cd 'any) -> sym
Changes the current directory to any. The old directory is returned on success, otherwise NIL. See also dir and pwd.


: (when (cd "lib")
   (println (sum lines (dir)))
   (cd @) )
10955
(cdaar 'lst) -> any
List access shortcut: Equivalent to (cdr (car (car 'lst))).


: (cdaar '(((1 2 3))))
-> (2 3)
(cdadr 'lst) -> any
List access shortcut: Equivalent to (cdr (car (cdr 'lst))).


: (cdadr '((1 2) (3 4)))
-> (4)
(cdar 'lst) -> any
List access shortcut: Equivalent to (cdr (car 'lst)).


: (cdar '((1 2) (3 4)))
-> (2)
(cddar 'lst) -> any
List access shortcut: Equivalent to (cdr (cdr (car 'lst))).


: (cddar '((1 2 3 4)))
-> (3 4)
(cddddr 'lst) -> any
List access shortcut: Equivalent to (cdr (cdr (cdr (cdr 'lst)))). Returns all but the first four elements of lst.


: (cddddr (1 2 3 4 5 6))
-> (5 6)
(cdddr 'lst) -> any
List access shortcut: Equivalent to (cdr (cdr (cdr 'lst))). Returns all but the first three elements of lst.


: (cdddr (1 2 3 4 5 6))
-> (4 5 6)
(cddr 'lst) -> any
List access shortcut: Equivalent to (cdr (cdr 'lst)). Returns all but the first two elements of lst.


: (cddr (1 2 3 4 5 6))
-> (3 4 5 6)
(cdr 'lst) -> any
List access: Returns all but the first element of lst.


: (cdr (1 2 3 4 5 6))
-> (2 3 4 5 6)
(center 'cnt|lst 'any ..) -> sym
Returns a transient symbol with all any arguments packed in a centered format. Trailing blanks are omitted. See also align, tab and wrap.


: (center 4 12)
-> " 12"
: (center 4 "a")
-> " a"
: (center 7 "a")
-> "   a"
: (center (3 3 3) "a" "b" "c")
-> " a  b  c"
(chain 'lst ..) -> lst
Concatenates (destructively) one or several new list elements lst to the end of the list in the current make environment. This operation is efficient also for long lists, because a pointer to the last element of the list is maintained. chain returns the last linked argument. See also link, yoke and made.


: (make (chain (list 1 2 3) NIL (cons 4)) (chain (list 5 6)))
-> (1 2 3 4 5 6)
(char) -> sym
(char 'cnt) -> sym
(char T) -> sym
(char 'sym) -> cnt
When called without arguments, the next character from the current input stream is returned as a single-character transient symbol, or NIL upon end of file. When called with a number cnt, a character with the corresponding unicode value is returned. As a special case, T is accepted to produce a byte value greater than any first byte in a unicode character (used as a top value in comparisons). Otherwise, when called with a symbol sym, the numeric unicode value of the first character of the name of that symbol is returned. See also peek, skip, key, line, till and eof.


: (char)                   # Read character from console
A                          # (typed 'A' and a space/return)
-> "A"
: (char 100)               # Convert unicode to symbol
-> "d"
: (char T)                 # Special case, catch all
-> # (not printable)
: (char "d")               # Convert symbol to unicode
-> 100
(chdir 'any . prg) -> any
Changes the current directory to any with cd during the execution of prg. Then the previous directory will be restored and the result of prg returned. See also dir and pwd.


: (pwd)
-> "/usr/abu/pico"
: (chdir "src" (pwd))
-> "/usr/abu/pico/src"
: (pwd)
-> "/usr/abu/pico"
(check 'sym ['fun]) -> num
Checks a database tree node (and recursively all sub-nodes) for consistency. Returns the total number of nodes checked. Optionally, fun is called with the key and value of each node, and should return NIL for failure. See also tree and root.


: (show *DB '+Item)
{C} NIL
   sup (7 . {7-3})
   nr (7 . {7-1})    # 7 nodes in the 'nr' tree, base node is {7-1}
   pr (7 . {7-4})
   nm (77 . {7-6})
-> {C}
: (check '{7-1})     # Check that node
-> 7
(chop 'any) -> lst
Returns any as a list of single-character strings. If any is NIL or a symbol with no name, NIL is returned. A list argument is returned unchanged.


: (chop 'car)
-> ("c" "a" "r")
: (chop "Hello")
-> ("H" "e" "l" "l" "o")
(circ 'any ..) -> lst
Produces a circular list of all any arguments by consing them to a list and then connecting the CDR of the last cell to the first cell. See also list.


: (circ 'a 'b 'c)
-> (a b c .)
(class sym . typ) -> obj
Defines sym as a class with the superclass(es) typ. As a side effect, the global variable *Class is set to obj. See also extend, dm, var, rel, type, isa and object.


: (class +A +B +C +D)
-> +A
: +A
-> (+B +C +D)
: (dm foo> (X) (bar X))
-> foo>
: +A
-> ((foo> (X) (bar X)) +B +C +D)
(clip 'lst) -> lst
Returns a copy of lst with all white space characters or NIL elements removed from both sides. See also trim.


: (clip '(NIL 1 NIL 2 NIL))
-> (1 NIL 2)
: (clip '(" " a " " b " "))
-> (a " " b)
(close 'cnt) -> flg
Closes a file descriptor cnt, and returns non-NIL when successful. See also open, listen and connect.


: (close 2)                            # Close standard error
-> T
(cnt 'fun 'lst ..) -> cnt
Applies fun to each element of lst. When additional lst arguments are given, their elements are also passed to fun. Returns the count of non-NIL values returned from fun.


: (cnt cdr '((1 . T) (2) (3 4) (5)))
-> 2
(collect 'var 'cls ['hook] ['any|beg ['end [var ..]]])
Returns a list of all database objects of class cls, where the values for the var arguments correspond to the any arguments, or where the values for the var arguments are in the range beg .. end. var, cls and hook should specify a tree for cls or one of its superclasses. If additional var arguments are given, the final values for the result list are obtained by applying the get algorithm. See also db, aux, fetch, init and step.


: (collect 'nr '+Item)
-> ({3-1} {3-2} {3-3} {3-4} {3-5} {3-6} {3-8})
: (collect 'nr '+Item 3 6 'nr)
-> (3 4 5 6)
: (collect 'nr '+Item 3 6 'nm)
-> ("Auxiliary Construction" "Enhancement Additive" "Metal Fittings" "Gadget Appliance")
: (collect 'nm '+Item "Main Part")
-> ({3-1})
(commit ['any] [exe1] [exe2]) -> flg
Closes a transaction, by writing all new or modified external symbols to the database, and by removing all deleted symbols from the database. For nested transactions, only the changes since the last call to begin are taken into account. A non-NIL any argument forces modifications of the current transaction level to be written out, even if this is not the top level. When any is anything other than T, it is implicitly sent (with all modified objects) via the tell mechanism to all family members. If exe1 or exe2 are given, they are executed as pre- or post-expressions while the database is locked and protected. Returns T when the topmost transaction is closed. See also rollback.


: (pool "db")
-> T
: (put '{1} 'str "Hello")
-> "Hello"
: (commit)
-> T
(con 'lst 'any) -> any
Connects any to the first cell of lst, by (destructively) storing any in the CDR of lst.


: (setq C (1 . a))
-> (1 . a)
: (con C '(b c d))
-> (b c d)
: C
-> (1 b c d)
(conc 'lst ..) -> lst
Concatenates all argument lists (destructively). See also append.


: (setq  A (1 2 3)  B '(a b c))
-> (a b c)
: (conc A B)                        # Concatenate lists in 'A' and 'B'
-> (1 2 3 a b c)
: A
-> (1 2 3 a b c)                    # Side effect: List in 'A' is modified!
(cond (('any1 . prg1) ('any2 . prg2) ..)) -> any
Multi-way conditional: If any of the anyN conditions evaluates to non-NIL, prgN is executed and the result returned. Otherwise (all conditions evaluate to NIL), NIL is returned. See also nond, if, if2 and when.


: (cond
   ((= 3 4) (println 1))
   ((= 3 3) (println 2))
   (T (println 3)) )
2
-> 2
(connect 'any 'cnt) -> cnt | NIL
Tries to establish a TCP/IP connection to a server listening at host any, port cnt. any may be either a hostname or a standard internet address in numbers-and-dots notation. Returns a socket descriptor cnt, or NIL if the connection cannot be established. See also listen and nagle.


: (connect "localhost" 4444)
-> 3
(cons 'any ['any ..]) -> lst
Constructs a new list cell with the first argument in the CAR and the second argument in the CDR. If more than two arguments are given, a corresponding chain of cells is built. (cons 'a 'b 'c 'd) is equivalent to (cons 'a (cons 'b (cons 'c 'd))).


: (cons 1 2)
-> (1 . 2)
: (cons 'a '(b c d))
-> (a b c d)
: (cons '(a b) '(c d))
-> ((a b) c d)
: (cons 'a 'b 'c 'd)
-> (a b c . d)
(copy 'any) -> any
Copies the argument any. For lists, the top level cells are copied, while atoms are returned unchanged.


: (=T (copy T))               # Atoms are not copied
-> T
: (setq L (1 2 3))
-> (1 2 3)
: (== L L)
-> T
: (== L (copy L))             # The copy is not identical to the original
-> NIL
: (= L (copy L))              # But the copy is equal to the original
-> T
(count 'tree) -> num
Returns the number of nodes in a database tree. See also tree and root.


: (count (tree 'nr '+Item))
-> 7
(ctl 'sym . prg) -> any
Waits until a write (exclusive) lock (or a read (shared) lock if the first character of sym is "+") can be set on the file sym, then executes prg and releases the lock. If the files does not exist, it will be created. When sym is NIL, a shared lock is tried on the current innermost I/O channel, and when it is T, an exclusive lock is tried instead. See also in, pipe and out. Note: It is not recommended to lock a file by name and then do I/O on the same file, as this may give unexpected results on some operating systems.


$ echo 9 >count                           # Write '9' to file "count"
$ ./p dbg.l
: (ctl ".ctl"                             # Exclusive control, using ".ctl"
   (in "count"
      (let Cnt (read)                     # Read '9'
         (out "count"
            (println (dec Cnt)) ) ) ) )   # Write '8'
-> 8
:
$ cat count                               # Check "count"
8
(ctty 'sym|pid) -> flg
When called with a symbolic argument, ctty changes the current TTY device to sym. Otherwise, the local console is prepared for serving the remote Pico Lisp process pid. See also raw.


: (ctty "/dev/tty")
-> T
(curry lst . fun) -> fun
Builds a new function from the list of symbols lst and the functional expression fun. Each member in lst that is a pat? symbol is immediately substituted inside fun by its value. All other symbols in lst are collected into a job environment.


: (de multiplier (@X)
   (curry (@X) (N) (* @X N)) )
-> multiplier
: (multiplier 7)
-> ((N) (* 7 N))
: ((multiplier 7) 3))
-> 21

: (let (N1 0  N2 1)
   (def 'fiboCounter
      (curry (N1 N2) (Cnt)
         (do Cnt
            (println
               (prog1
                  (+ N1 N2)
                  (setq N1 N2  N2 @) ) ) ) ) ) )
-> fiboCounter
: (pp 'fiboCounter)
(de fiboCounter (Cnt)
   (job '((N2 . 1) (N1 . 0))
      (do Cnt
         (println
            (prog1 (+ N1 N2) (setq N1 N2 N2 @)) ) ) ) )
-> fiboCounter
: (fiboCounter 5)
1
2
3
5
8
-> 8
: (fiboCounter 5)
13
21
34
55
89
-> 89
(cut 'cnt 'var) -> lst
Pops the first cnt elements (CAR) from the stack in var. See also pop and del.


: (setq S '(1 2 3 4 5 6 7 8))
-> (1 2 3 4 5 6 7 8)
: (cut 3 'S)
-> (1 2 3)
: S
-> (4 5 6 7 8)