Write a program in common lisp, that prints the average value of the second column of a…
- User
Write a program in common lisp, that prints the average value of the second column of a CSV file.
Assistant
Assuming that the CSV file has the following format:
codecolumn1,column2,column3 value1,value2,value3 value4,value5,value6Here's a possible implementation of the program:
code(defvar *csv-file* "example.csv") (defvar *column-index* 2) (defvar *total* 0) (defvar *count* 0) (defun read-csv-file (filename) (with-open-file (in *csv-file*) (loop for line = (read-line in) do (setf *count* (1+ *count*)) (setf (aref line *column-index*) (parse-float (subseq line 0 *column-index*))) (setf *total* (* *total* (parse-float (subseq line *column-index*))))))) (defun get-average (filename) (/ (* (read-csv-file filename) *column-index*) *count*) (get-average *csv-file*)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.