Generalized Assignment:  setf

 

e.g.:

 

(setf a ‘(1 2 3 4))

 

(setf  (nth 2 a)  ‘(1 2 3 4))

 

 

 

Conditional expressions:

 

 

Tail-Recursive functions:

 

e.g.:

 

int  fact(int n)

{

          int i, product = 1;

 

          for (i=1; i <= n; i++)

                     product = product * i;

 

          return product;

}

 

 

 

int fact(int n)

{

 

          if (n <= 0)  return 1;

          else return (fact(n-1)*n);

}

 

 

int  tr_fact(int n)

{

          return tr_fact1(n, 1);

}

 

int  tr_fact1(int n,  int result)

{

          if (n <= 0)  return result;

          else return tr_fact1(n-1, result=result * n);

}

 

 

 

(defun  add(inList)

 (cond   ((null inList)  0)

            (t   (+  (car  inList)  (add  (cdr  inList))))

))

 

 

(defun  tr_add(inList)

 

  (tr_add1   inList  0)

)

 

(defun tr_add1(inList  result)

 

(cond  ((null inList)  result)

           (t   (tr­_add1  (cdr  inList)   (setf   result   (+  result  (car inList) ))))))

 

 

 

(setunion   ‘(a b c)  ‘(a d e))  =>  (a b c d e)

 

 

 

(setunion  ‘(1   2  (3  4 5))  ‘(1   2   (3   4)))  =>   (1   2  (3  4  5)  (3  4))

       

 

 

 

(deleteall ‘a  ‘(  a   b  (a  b)  (((a)))  ))   =>    (  b    (b)    ((()))    )   

                                                          =>    (b   (b)   ((nil))  )

 

 

 

 

(a   b)     is equal to   (c   d)

 

is not equal to   ( a  (b)  )  

 

 

 

is not equal to  (a   b   c)

 

 

Example of dolist:

 

(defun  add+  (inList)

(let  ((result  0)) 

(dolist   (ele   inList  result) 

(cond  ((> ele  0)  (setf   result (+  result ele)))

))))

 

 

 

 

 

Applicative Programming:

 

(apply   #’+  ‘(1  2  3))  =>  6

 

(funcall  #’+   1  2  3)  => 6

 

 

(mapcar   #'(lambda  (x)  (+ x 1))   '(1  2  3  4  5))  => (2 3 4 5 6)