•

Check out our Community Blogs Register and join over 40,000 other developers!

### Popular Tags      # [LISP] Arithmetic Average of Elements LISP/CLISP

clisp lisp list arithmetic average element arithmetic-average

Best Answer sepp2k, 19 March 2013 - 08:54 AM

You're still calling the list function when you do (sum (list)) and (length (list)). Calling the list function with no arguments produces an empty list. So you're dividing the sum of the empty list (i.e. 0) by the length of the empty list (i.e. 0). That's a division by zero. If you remove the parentheses around "list", it should work fine. Go to the full post This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

### #1 MaxBummer

MaxBummer

Posted 19 March 2013 - 07:05 AM

I have to write a function that computes the arithmetic average of the elements of a list. Basically the arithmetic average is the sum of elements over the number of elements, that is the sum of elements over the length of the list.. So you take the head of the list with car list and keep adding it and you have the sum until there are no more elements in the list ( when the list is NULL)

I tried to code it but I got stuck

```(defun ave (list)
(if (NULL list) ave
(/ (= sum (+ car (list) sum)) (length (list)))
)
)
```

What am I doing wrong? Thanks.

Edited by MaxBummer, 19 March 2013 - 07:11 AM.

### #2 sepp2k

sepp2k

Posted 19 March 2013 - 07:18 AM

Trying to run the code on different inputs, should produce error messages that give you some idea of what you're doing wrong.

Some problems that I see are: You're using the variables ave and sum, which aren't defined anywhere and on line 3 you have a backslash that makes no sense. You're also calling the function "list" in a couple of places where you probably intended to call another function with the variable "list" as its arguments. For example "car (list)" was probably intended to be "(car list)".

### #3 MaxBummer

MaxBummer

Posted 19 March 2013 - 07:41 AM

Well what I want is to start from sum=0 and ave is the average I want to compute. The sum should keep increasing as long as there are elements in the list. The code below says that sum has no value, though how do I make it start from sum =0 and increase (= sum (+ sum 1)) when length increases. Or something like that.. Whatever I do it says sum has no value. Am I just complicating things?

```(defun ave (list1)
(if (NULL list1)  ave
(if (NULL sum)
(/ (= sum (+ (car list1) sum)) (length (list1)))
)
)
)```

Edited by MaxBummer, 19 March 2013 - 07:42 AM.

### #4 sepp2k

sepp2k

Posted 19 March 2013 - 07:51 AM

sum doesn't have a value because you never define it anywhere. So you never give it a value. Same for ave.

Note that = is an equality operator. It checks whether two values are equal and returns a boolean value accordingly. It does not change the value of any variables nor does it define new variables.

Also note that your function contains no kind of loop or recursion or any call to a function that iterates over a list (except length), so there's no way it will sum the contents of the list.

One step to simplifying your code would be to define two functions: sum and ave. sum would then simply sum the contents of a list (using recursion, a loop or a call to some function that iterates over the list - like reduce) and ave can then call sum and divide the result by the length of the list.

Edited by sepp2k, 19 March 2013 - 07:51 AM.

### #5 MaxBummer

MaxBummer

Posted 19 March 2013 - 08:11 AM

Would this sum function do?

```(defun sum (list1)
(if (NULL list1) 0
(+ sum (car list1))
)
)

(defun ave (list2)
(if (NULL list2) 0
(= ave (/ sum (length (list2))))
)
)

```

And if I were to make them work together in the same program, how can I do that?

### #6 sepp2k

sepp2k

Posted 19 March 2013 - 08:18 AM

Your sum function is broken because it still uses sum as a variable (and does not recurse).

Your average function still uses = in a way that makes no sense, uses sum as a variable and list2 as a function.

You should familiarize yourself with Common Lisp syntax before you continue further.

### #7 WingedPanther73

WingedPanther73

Posted 19 March 2013 - 08:41 AM

Usually, the SUM of a list is the first element of the list + the SUM of the rest of the list.

Usually, the average doesn't check the equality of anything with anything else, it just divides the SUM of the list with the LENGTH of the list.

One thing that's clear is you are not thinking of lisp syntax as "(function_name param1 param2 param3 ...)" The result is you're tossing around sum outside of parenthesis, which means you're treating as data, not a function.

You're on the right track of having a sum function and an ave function. Before attempting to implement your ave function, make sure your sum function works. Have you tested it with sample data yet? Does it work? If not, any attempt at ave will fail miserably.

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #8 MaxBummer

MaxBummer

Posted 19 March 2013 - 08:51 AM

```(defun sum (list)
(if (NULL list) 0
(+ (car list) (sum (cdr list)))
)
)

(defun ave (list)
(if (NULL list) 0
(/ (sum (list)) (length (list)))
)
)
```

### #9 sepp2k

sepp2k

Posted 19 March 2013 - 08:54 AM   Best Answer

You're still calling the list function when you do (sum (list)) and (length (list)). Calling the list function with no arguments produces an empty list. So you're dividing the sum of the empty list (i.e. 0) by the length of the empty list (i.e. 0). That's a division by zero. If you remove the parentheses around "list", it should work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download