Scheme less than average function
scheme comparison operators
scheme average function
scheme functions list
I want to create a function in Scheme that takes a list of numbers and displays the number that are less than the average. The answer should be 3 but returns 2. I believe it is not reading "15." What am I doing wrong?
(define x (list 10 60 3 55 15 45 40)) (display "The list: ") (let ((average (/ (apply + (cdr x)) (car x)))) (length (filter (lambda (x) (< x average)) (cdr x))))
The list: (10 60 3 55 15 45 40)
The average: 32.57
Number of values less than average: 2
Sure, let's do this step by step!
First off, let's define a function to get us the average of a list. We'll call this function
(define (mean lst) (/ (apply + lst) (length lst)))
We get the average by adding all the numbers together and dividing that sum by how many numbers were in the list (that is to say, the length of the list). There are Racket libraries that could provide us with this function, such as the Statistics Functions library from the math-lib package. But we'll do it ourselves since it's simple enough.
Next we have the meat of our algorithm, where we define a function that takes a list, gets the average, and filters out every element less than the average.
(define (less-than-average lst) (filter (λ (x) (< x (mean lst))) lst))
Looks pretty similar to your code, right? Let's see if it works:
(less-than-average (list 10 60 3 55 15 45 40))
I ran this in DrRacket and it gave me 10 3 15, which is the correct answer. So why did this work, when you (very similar) code does not?
The problem with your code is that the value you are storing in
average is incorrect. Viz,
(define x (list 10 60 3 55 15 45 40)) (let ((average (/ (apply + (cdr x)) (car x)))) average)
evaluates to 21.8. As you state, the correct average is 32.57. Your current technique for computing this average is to add up everything in the list after the first element (that's what
(apply + (cdr x)) does) then dividing that sum by the first element. This will not give you the mean value of the list. What you ought to be doing is summing up the entire list (via
(apply + x)), then dividing that by how many numbers were in the list (ie
Scheme greater than average counter : scheme, I need to make a function that counts the amount of integers that are greater than the average. I already created the average function it is below. I think I should� These formulas return the average price of properties where: prices are greater than zero; prices are greater than zero and less than $500,000; properties have at least 2 bedrooms and more than 1 bathroom; Double quotes ("") in criteria. In general, text values in criteria are enclosed in double quotes (""), and numbers are not.
This answer tries to pay attention to performance. The other answer by Alex has a mean function which walks the list twice: once to add up the elements, and once to compute the length. It then calls this function for every element of the list when filtering it, resulting a function which takes time quadratic in the length of the list being averaged. This is not a problem for small lists.
Here is a mean function which walks the list once.
(define (list-average l) (let average-loop ([tail l] [sum 0] [len 0]) (if (null? tail) (/ sum len) (average-loop (rest tail) (+ sum (first tail)) (+ len 1)))))
This is a little better than one which walks it twice, but the difference is probably not significant (naively it might be twice as fast, in practice probably less).
Here is a filtering function which is careful to call the mean function only once. This is a whole complexity class faster than one which calls it for every element, resulting in a function which takes time proportional to the length of the list, not the length of the list squared.
(define (<=-average l) (define average (list-average l)) (filter (λ (e) (<= e average)) l))
[PDF] Scheme Tutorial Solutions, Scheme Tutorial Solutions Functions which average the values in a list Function which removes all items from a list which are greater than a user pro-. Many programmers consider that to be easier to read than HTML. List manipulation . There are five primitive functions for manipulating lists and pairs in Scheme. They are cons, car, cdr, set-car! and set-cdr! > (define test (cons 1 2)) #<unspecified> > (car test) 1 > (cdr test) 2
I will not comment too much, I have just written such a function here and you can study it:
(define less-than-average (lambda (list return) ((lambda (self) (self self list 0 0 return)) (lambda ( self list acc n col) (if (null? list) (col (/ acc n) list) (let ((a (car list))) (self self (cdr list) (+ acc a) (+ n 1) (lambda (average l*) ; when the average is known, (if (< a average) (col average (cons a l*)) (col average l* ))))))))))
1 ]=> (less-than-average (list 10 60 3 55 15 45 40) (lambda (avg l*) (newline) (display avg) (newline) (display l*) (newline)) ) 228/7 (10 3 15)
CSE 341 -- Scheme Basics, But good style is to write #t or #f whenever you mean true or false, and to Scheme provides us with a uniform syntax for invoking functions: Scheme waits for the user to type an expression, reads it, evaluates it, and prints the return value. expressionQ) ;; reverse a list and double it ;; less efficient version: ( define (r2 x)� > (average (+ 10 8) (* 3 5)) 16.5 > (average (average 2 3) (average 4 5)) 3.5 > (sqrt (average 143 145)) 12 Any return value can be used as an end in itself, as the return value from sqrt was used in the last of these examples, or it can provide an argument to another procedure, as the return value from * was used in the first of these examples.
Scheme, A Scheme expression is a construct that returns a value, such as a variable reference, The operator and operand expressions are evaluated (in an unspecified In Scheme, combinations must have at least one subexpression, so () is not a ((x '(1 3 5 7 9))) (do ((x x (cdr x)) (sum 0 (+ sum (car x)))) ((null? x) sum))) => 25. Vectors are heterogenous structures whose elements are indexed by integers. A vector typically occupies less space than a list of the same length, and the average time required to access a randomly chosen element is typically less for the vector than for the list. The length of a vector is the number of elements that it contains. This number is
[PDF] Defining Scheme Functions, Here's the Scheme version of a function that determines whether or not a year is a leap year: If the list is empty, then the sum is 0. ; Otherwise, the The first of the two lists contains all of those element less than the. ;; pivot� Empty cells are ignored in range and average_range when calculating averages. AVERAGEIF returns #DIV/0! if no cells in range meet criteria. Average_range does not have to be the same size as range. The top left cell in average_range is used as the starting point, and cells that correspond to cells in range are averaged.
- Nice! Thank you!!
- @jhnorr see What should I do when someone answers my question?, What does it mean when an answer is "accepted"?, How does accepting an answer work?.
- Walking the list twice has the same big O as once, but you have a valid point that Alex should have cached the mean and used it for the filter.
- @Sylwester yes, that's what I said: the difference in the performance of the mean function is 'probably not significant', but calling it for every element in the filter is a complexity class worse.