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
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
- Managed C++
- Visual Basic 4 / 5 / 6
- linked list
- hello world
[LISP] Arithmetic Average of Elements LISP/CLISPclisp lisp list arithmetic average element arithmetic-average
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.
Posted 19 March 2013 - 07:18 AM
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)".
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.
Posted 19 March 2013 - 07:51 AM
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.
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?
Posted 19 March 2013 - 08:18 AM
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.
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.
My MineCraft server site: http://banishedwings.enjin.com/
Posted 19 March 2013 - 08:51 AM
What about this? I fixed everything I should and it says that there's division by 0..
(defun sum (list) (if (NULL list) 0 (+ (car list) (sum (cdr list))) ) ) (defun ave (list) (if (NULL list) 0 (/ (sum (list)) (length (list))) ) )
Posted 19 March 2013 - 08:54 AM Best Answer