Lisp



nil = () = false
t = not empty list = true
 '  means do not evaluate
> (car '(1 2 3))  ;returns first element of the list
1


> (cdr '(1 2 3))  ;returns all elements of the list except the first
(2 3)

> (cons '1  '(2 3 4))    ;inserts the first argument to the second argument which should be a list
(1 2 3 4)

(list Sex)  <==> (cons Sex ())
 
(list Sex1 Sex2) <==> (cons Sex1 (cons Sex2 nil))
 

> (append '(1 2) '(3 4 5))      ;appends the first list to the second list
(1 2 3 4 5)
> (append '(a) '(b))
(A B)

> (list arg1 arg2)     ; returns the list (arg1 arg2), they don't need to be lists
> (list '(a) '(b))
((A) (B))

> (listp '(1))     ;test if it is a list
T

> (member arg list_arg)     ; tests if arg is a member of the list_arg

> (atom arg)    ;tests if the argument is an atom

> (numberp arg)    ;tests if the argument is a number
> (integerp arg)
>(floatp arg)

> (null ())    ;checks if the argument is null
T


> (eq arg1 nil)  : test if it is null  ===   (null arg1)

> (and arg1 arg2 ... argn)

>(or arg1 arg2 ... argn)

>(not arg)

>
> (reverse '(1 (a b) (c d) 4))
(4 (c d) (a b) 1)

> (cond     ;tests conditions one by one and does not check other conditions right after the first condition is satisfied
        (
            (null (cdr arg1))      ;condition
            (addCXRow  (list (car arg1)) (list (car arg2)))       ;return value
        )
        (
            t                     ;condition
            (+ arg 1 )   ;return value
        )
    )

> (if (null (cdr arg1))
          ()
          (car arg1)
)

> (set 'carnivores '(lion tiger leopard))     ====      (setq carnivores '(lion tiger leopard))
> carnivores
(LION TIGER LEOPARD)

if you have a list of lists and want to sort based on the third element of the child lists:  (sort openList #'< :key #'third)
OR to sort based on fVal (which is a function of gVal+hVal)   (sort openList #'< :key #'fVal)

Functions

(defun getLast (arg)      ;define a function
    (if      ;if condition + if clause + else clause
        (eq (cdr arg) nil)    ;equal?
        (car arg)
        (getLast (cdr arg) )
     )
)




mapcar

> (defun addone (arg)
    (+ arg 1)
)
> (mapcar #'addone '(1 2 3) )
(2 3 4)

;This is the same as the following

> (mapcar #'(lambda (n) (+ n 1)) '(1 2 3))
(2 3 4)

mapcar with more than one argument
> (mapcar '+ '(1 2 3 4 5) '(3 4 5 6 7))
(4 6 8 10 12)

(mapcan (lambda (x) (if (> x 0) (list x) nil)) '(-4 6 -23 1 0 12 )) => (6 1 12)
only returns non-nils


Notice that, if the supplied lists are of varying lengths, mapcar will stop evaluation when it reaches the end of the shortest list

(defun mymapcar (f lis)
    (cond
        (null nil)
        nil
    )
    ('else
        (cons
            (funcall
                f
                (car lis)
            )
            (mypac
                f
                (cdr lis)
            )
        )
)


Executing a Sequence of Commands (prog)

There often arise occasions in which a LISP program needs to contain a sequence of statements. To handle this, the PROG feature was created, which also allows creation of local variables, labels and gotos, and abnormal exits.

The PROG construct allows for an indefinite number of forms to be evaluated with local variables. In
modern LISP it has been replaced by LET and LOOP constructs, but it is still useful.
 
  (prog (var1 var2 ... varn) <forms>)
 
  1. The number of forms in prog is indeterminate.
  2. Prog initializes all its local variables to nil (var1 var2 ... varn).
  3. <Forms> are evaluated in order except:
    a. Atomic forms are not evaluated. They are treated as labels.
    b. (GO label) means go back to the form that follows label & continue.
    c. (return expr) exits prog evaluation & returns the (eval expr) as the value of the prog.
  4. If prog runs out of forms, it returns nil.
  5. If a cond runs out of stuff inside a prog it is treated as a NOOP.

(defun fact (n)
    (prog (i j)
        (if
             (< n 0)
             (return nil)
          )
          (setf i 1 j 1)
          (if
               (< n 2)
               (return j)
            )
            LOOP
            (setf i (+ i 1) j (* j i) )
            (if
                  (= i n)
                  (return j)
              )
              (go LOOP)
         )
)

> (list-length '(a c v))
3

> (dolist (x '(a b c)) (print x))

A
B
C
NIL

Properties

(putprop 'symbol 'property_value 'property_name)
(putprop 'cain 'adam 'father)                            =====                    (setf (get 'cain 'father) 'adam)
(putprop 'eve (cain abel) 'mother-of)                 =====                    (setf (get 'eve 'mother-of) '(cain abel))
(defun putprop (atm pvalue pname) (setf (get atm pname) pvalue)


to get the property value

(get 'cain 'father)
(remprop 'cain 'father)




USER(59): (nth 0 '(a b c d))
A
USER(60): (nth 2 '(a b c d))
C


IO and Files

(load "predicateListFile.lsp")
;---------------------------------------------------------------------------other random code

(DEFINE (FLATTEN (LAMBDA (L))
    (COND
    ((NULL L) NIL)
    ((ATOM L) (LIST L))
    (T (CONC (FLATTEN (CAR L))
             (FLATTEN (CDR L))))))))




Replace an element in list
;(setElement '(1 2 3) 1 10)
(defun setElement (L N val)
(let ((myList L))
(setf (nth N myList) val); <----
myList
)
)

(1 10 3)
-----
swap indexed elements
(defun swapList (mylist i j)
    (cond
        (
            (< i 0)
            nil
        )
        (
            (< j 0)
            nil
        )       
        (
            t
            (prog ()
                (setf temp (nth i mylist))
                (setElement myList i (nth j myList))
                (setElement myList j temp)
               
                (return myList)
            )
        )
    )
)

---------------

> (position 3 '(1 2 5 6 3))
4



ċ
astar.lsp
(2k)
Morteza Sh-,
Oct 26, 2011, 3:04 PM
ċ
astarclass24.lsp
(1k)
Morteza Sh-,
Oct 26, 2011, 3:04 PM
ċ
class24AStar.pdf
(581k)
Morteza Sh-,
Oct 26, 2011, 3:28 PM
Ċ
Morteza Sh-,
Oct 13, 2011, 9:52 PM
Ċ
Morteza Sh-,
Oct 13, 2011, 9:53 PM
Ċ
Morteza Sh-,
Oct 13, 2011, 9:53 PM
Comments