81 lines
2.1 KiB
Common Lisp
81 lines
2.1 KiB
Common Lisp
;;; constant timem to access
|
|
|
|
(make-array 5 :initial-element 1)
|
|
(make-array 6 :initial-contents `(a e i o u j)) ; should match with size
|
|
(make-array 3) ; elements are implementation dependent
|
|
|
|
*print-array*
|
|
|
|
;;; hash tables - more performant, but a bit more complicated api than alist
|
|
(setf my-table (make-hash-table))
|
|
(gethash 'a my-table)
|
|
(setf (gethash 'a my-table) '(mary doctor))
|
|
|
|
;;; property list
|
|
;; (ind-1 value-1 ind-2 value-2 ...)
|
|
;; so all on one level instead of cons or lists
|
|
;; GET function retrieves values from property list of a symbol.
|
|
;; so first we put some properties to symbol
|
|
(setf (get 'fred 'sex) 'male)
|
|
(setf (get 'fred 'age) 33)
|
|
(get 'fred 'sex)
|
|
(describe 'fred) ; plist is printed in repl
|
|
|
|
(remprop 'fred 'age)
|
|
|
|
(symbol-plist 'fred)
|
|
|
|
(defun subprop (symbol item property)
|
|
(setf (get symbol property)
|
|
(remove item (get symbol property)))) ; well that's exercise for lisp properties, ok
|
|
|
|
(setf (get 'fred 'syblings) '(jeorge tom))
|
|
(get 'fred 'syblings)
|
|
(subprop 'fred 'tom 'syblings)
|
|
|
|
;; allright, let's do exercise about random and histogram
|
|
;;
|
|
;; (new-histogram 11)
|
|
;; (dotimes 200
|
|
;; (record-value (random 11)))
|
|
;; (print-histogram)
|
|
|
|
(defvar *hist-array*)
|
|
(defvar *total-points*)
|
|
|
|
(defun new-histogram (size)
|
|
(setf *hist-array* (make-array size :initial-element 0))
|
|
(setf *total-points* 0))
|
|
|
|
(new-histogram 11)
|
|
|
|
(length *hist-array*)
|
|
(aref *hist-array* 1)
|
|
(incf (aref *hist-array* 1))
|
|
|
|
(defun record-value (n)
|
|
"Adds value h to corresponding bucket of *HIST-ARRAY* and increases *TOTAL-POINTS*."
|
|
(if (> n (length *hist-array*)) (error "nubmer is too big"))
|
|
(incf (aref *hist-array* n))
|
|
(incf *total-points*))
|
|
|
|
;; next - print
|
|
|
|
(defun print-hist-line (i)
|
|
(let* ((hist-value (aref *hist-array* i))
|
|
(hist-line (make-string hist-value :initial-element #\*)))
|
|
(format t "~&~2S [ ~3S] ~S" i hist-value hist-line)))
|
|
|
|
(dotimes (i 200)
|
|
(record-value (random (length *hist-array*))))
|
|
|
|
(print-hist-line 2)
|
|
|
|
(defun print-histogram ()
|
|
(dotimes (i (length *hist-array*))
|
|
(print-hist-line i)))
|
|
|
|
(print-histogram)
|
|
|
|
;;; yay!
|