CIS 400 LECTURE 13

 

Recursion in Lisp:

 

mn = m * mn-1, for n > 0

     = 1, for n = 0

 

>(defun expon (m n)

       (cond ((zerop n) 1)

                  (t * m(expon m(1- n)))

        )

   )

 

;;;;;;;;;;;;;  counting atoms in a nested list  ;;;;;;;;;;;;;;;;;;;

 

>(defun count-atom (L)

        (cond ((null L) 0)              ; empty list

                  ((atom L) 1)                       ; not a list, single atom

                   (t (+ (count-atom (car L))

                           (count-atom (cdr L))

                       )

         )

  )

 

>(count-atom ((a b) c (d e)))

5

>(trace count-atom)                              ; traces function

>(untrace count-atom)                          ; turns trace off

 

>(apply '+ '(2 3))

5

>(apply 'cons '(3(4 5)))

(3 4 5)

 

;;;;;;;;;;;;;;;;;;;  iterator in Lisp  ;;;;;;;;;;;;;;;;;;;

 

>(mapcar 'oddp '(1 2 3))                      ;  apply "oddp" to successive "car's" of the list

(t nil t)

>(defun square (x)

      (* x x)

   )

>(mapcar 'square '(1 2 3 4 5))

(1 4 9 16 25)

 

 

 

>(setq words '((one eins)                      ; for translating english to german

                        (two zwei)

                        (three drei)

                       )

  )

 

>(mapcar 'car words)

(one two three)

 

>(mapcar 'cdar words)

(eins zwei drei)

 

>(mapcar 'reverse words)

((eins one) (zwe two) (drei three))

 

;;;;;;;;;;;;;;;  THE FOLLOWING IS NOT IN XLISP  ;;;;;;;;;;;;;;;;;;;;

 

>(mapcar 'apply '(+ - *) '((1 2) (3 4) (5 6)))

(3 -1 30)

 

;;;;;;;;;;;;;;;  LAMBDA NOTATIONS  ;;;;;;;;;;;;;;;;;;;;;;

 

>(mapcar #'(lambda (x) (* x x)) '(1 2 3 4 5))               

(1 4 9 16 25)                                 ; "#" tells interpreter that a function  is coming

 

>(mapcar #' (lambda(x) (* x 10)) '(1 2 3 4 5))

10 20 30 40 50

 

;;;;;;;;;;;;;;;;  pre-CommonLisp looping  ;;;;;;;;;;;;;;;;;

 

>(defun expon (m n)

       (prog ((result 1)

                  (exp n)

                 )

                 loop 1

                       (if (zerop exp) (return result))

                       (setq result (* m result))

                       (setq exp (1- exp))                    ; "1- exp" is like a decrementor

                 (go loop 1)                                      ; returns to "loop 1"

        )

  )

 

;;;;;;;;;;;;;;;;;;;  property lists  ;;;;;;;;;;;;;;;;;;;;;;;;;

 

>(putprop 'fred 'male 'sex)                                ; "fred" has "sex" property of "male"

>(symbol-plist 'fred)                                         ; list "fred's" properties

(sex male)

 

>(get 'fred 'sex)                                                ; returns the property

male

 

>(remprop 'fred 'sex)                                        ; property is removed

nil

 

>(setf (get 'fred 'sex) 'female))               ; property changed

female

 

>(setf (symbol-plist 'fred) '(sex male age 23 (sibs geo sally lou)))           ; sets an entire                

(sex male age 23 sibes (geo sally lou))                                               ; property list

 

>(get 'fred age)

23

 

Moving on now to chapters 5 and 10

 

Elementary data types:

 

storage®[static]

·        Hardware-bit stream, (bytes or words)

·        In virtual computer

v     Arrays, stacks, char strings, (composite data types)

v     Pointers or fractions, (elementary data types)

 

Data objects

·        Run time grouping of 1 or more virtual computer data pieces

v     Programmer defined objects, (constants, variables)

v     System defined objects, (run-time stack)

 

Container for data values, (bit strings)

 

Data object attributes

·        Data type

·        Life time

·        Elementary (single value- manipulated only as a unit) 

             OR

      Structured (aggregates, more than one)

·        Participate in bindings

 

 

 

Built in types: useful properties

 

·        Invisibility of underlying representation, (i.e. not having to actually represent integer at the assembly level)

v     Readability improved

v     Improved portability

·        Correct use of variables-checked at compile time [static]

·        Disambiguation of operators-done at translation/compilation

·        Accuracy control