Website:
Paste Title:
Colors
Sizes
1
2
3
4
5
6
7
(declaim (inline gets)) (define-alien-routine gets (* char) (s (* char))) (let ((buf (make-array 8192 :element-type 'character))) (defun c-gets () (sb-sys:with-pinned-objects (buf) (with-alien ((res c-string (gets (sb-sys:vector-sap buf)))) res)))) #| This lops of the terminal '\n (don't know why) and returns a new string object for every line. For this application that's fine. But in general I'd prefer a version with parameter BUF that returned (a pointer to) BUF (that included, I assume, the terminal \n\0). Also, how can I do this with char *fgets_unlocked(char *, size_t, FILE *) ??? No idea how to convert between stdin and *STANDARD-INPUT*. |# (defconstant +max-species+ 10000) (defun main () (let* ((population (make-hash-table :size +max-species+ :test 'equal)) (num-trees (loop for tree = (c-gets) while tree count (incf (gethash tree population 0)))) (species (make-array (hash-table-count population) :fill-pointer 0))) (loop for s being each hash-key of population do (vector-push s species)) (loop for s across (sort species 'string<) do (format t "~a ~,6f~%" s (/ (* 100d0 (gethash s population)) num-trees))))) (main)
Password to view?
Enable code highlighting?
No
Yes