Skip to main content
deleted 41 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

"skewed" "Skewed" average in Lisp

I am trying to learn Lisp.

For that I set myself the task to calculate the average of a list, but with two conditions:

  1. negative numbers are ignored
  2. numbers greater than 100 are counted as if they were 100

So the "skewed" average of the list '(1 -3 42 297 14) should be (1 + 42 + 100 + 14) / 4 (157/4).

I wrote 2 functions that do that.

  Please review and comment.

(defun skewed-average1 (list)
    "calculate average by summing and dividing"
    (let ((sum 0) (n 0))
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf sum (+ x sum))
                    (setf n (+ 1 n)))))
         (/ sum n)))

(defun skewed-average2 (list)
    "calculate average by building 'fixed' list"
    (let (newlist)
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf newlist (cons x newlist)))))
        (/ (apply #'+ newlist) (length newlist))))

(let ((numbers '(1 -3 42 297 14)))
    (print (skewed-average1 numbers))
    (print (skewed-average2 numbers)))

Also, how should errors be treated? Imagine passing an empty list to the functions; or a list with all negative numbers.

"skewed" average in Lisp

I am trying to learn Lisp.

For that I set myself the task to calculate the average of a list, but with two conditions:

  1. negative numbers are ignored
  2. numbers greater than 100 are counted as if they were 100

So the "skewed" average of the list '(1 -3 42 297 14) should be (1 + 42 + 100 + 14) / 4 (157/4).

I wrote 2 functions that do that.

  Please review and comment.

(defun skewed-average1 (list)
    "calculate average by summing and dividing"
    (let ((sum 0) (n 0))
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf sum (+ x sum))
                    (setf n (+ 1 n)))))
         (/ sum n)))

(defun skewed-average2 (list)
    "calculate average by building 'fixed' list"
    (let (newlist)
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf newlist (cons x newlist)))))
        (/ (apply #'+ newlist) (length newlist))))

(let ((numbers '(1 -3 42 297 14)))
    (print (skewed-average1 numbers))
    (print (skewed-average2 numbers)))

Also how should errors be treated? Imagine passing an empty list to the functions; or a list with all negative numbers.

"Skewed" average in Lisp

I set myself the task to calculate the average of a list, but with two conditions:

  1. negative numbers are ignored
  2. numbers greater than 100 are counted as if they were 100

So the "skewed" average of the list '(1 -3 42 297 14) should be (1 + 42 + 100 + 14) / 4 (157/4).

I wrote 2 functions that do that. Please review and comment.

(defun skewed-average1 (list)
    "calculate average by summing and dividing"
    (let ((sum 0) (n 0))
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf sum (+ x sum))
                    (setf n (+ 1 n)))))
         (/ sum n)))

(defun skewed-average2 (list)
    "calculate average by building 'fixed' list"
    (let (newlist)
        (dolist (x list)
            (if (>= x 0)
                (progn
                    (if (> x 100) (setf x 100))
                    (setf newlist (cons x newlist)))))
        (/ (apply #'+ newlist) (length newlist))))

(let ((numbers '(1 -3 42 297 14)))
    (print (skewed-average1 numbers))
    (print (skewed-average2 numbers)))

Also, how should errors be treated? Imagine passing an empty list to the functions; or a list with all negative numbers.

Added "beginner" tag because "I am trying to learn"
Link
lightning_missile
  • 2.8k
  • 2
  • 24
  • 42
Source Link
pmg
  • 530
  • 3
  • 13
Loading