It seems that when I program in Lisp my brain goes on auto pilot and I end up solving the problem somehow. I don't even think I just do and it works out.
That said, this is some horrible Lisp code that was hacked together in about 20 mins, just to see if I could do it.
The C++ program that this is based off was about 150 lines long, so doing it in about 59 lines of terrible code is neat.
The date is 50 lines and is structured like 2 dates and 48 doubles 1 for each hour:
3/14/2015 3/15/2015 32.12 12.23 34.12 . . 56.12
(defun read-nth-line (file n &aux (line-number 0))
"Read the nth line from a text file. The first line has the number 1"
(assert (> n 0) (n))
(with-open-file (stream file)
(loop for line = (read-line stream nil nil)
if (and (null line) (< line-number n))
do (error "file ~a is too short, just ~a, not ~a lines long"
file line-number n)
do (incf line-number)
if (and line (= line-number n))
do (return line))))
(defun arithmetic-average (samples)
(/ (reduce #'+ samples)
(length samples)))
(defun get-file (filename)
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
collect line)))
;; compute sDev
(defun standard-dev (colc)
(let ((mean (arithmetic-average colc)))
(sqrt (* (/ 1.0d0 (length colc))
(reduce #'+ colc
:key (lambda (x)
(expt (- x mean) 2)))))))
(defun split-in-half (sequence)
(let ((mid (ceiling (length sequence) 2)))
(list (subseq sequence 0 mid)
(subseq sequence mid nil))))
(defun parse-string-to-float (line)
(with-input-from-string (s line)
(loop
:for num := (read s nil nil)
:while num
:collect num)))
(defun extract-float (line)
"doc"
(first (parse-string-to-float line)))
(defun process-file ()
"print mean and standard deviation to terminal"
(let* ((l1 (get-file "~/ClionProjects/project5withTemplates/twoday.txt"))
(date1 (first l1))
(date2 (second l1))
(day1temps (mapcar #'extract-float (first (split-in-half (rest (rest l1))))))
(day2temps (mapcar #'extract-float (second (split-in-half (rest (rest l1))))))
(s-dev-day-1 (standard-dev day1temps))
(s-dev-day-2 (standard-dev day2temps)))
(print "date:")(print date1) (print "standard deviation:") (print s-dev-day-1)
(print "date:")(print date2) (print "standard deviation:") (print s-dev-day-2)) nil)