家庭作业,要求用CDR 和 CAR 递归求一个非空数组的Median,巨多限制,具体如下,
请大牛帮忙,多谢。
Write a function median in Scheme that takes a nonempty list L of positive
integers (zero is not considered a positive integer) and other auxiliary
parameters of your choice. The list L is flat (i.e., it does not contain
sublists) and it is sorted in ascending order. If the length of list L is
odd, the function median returns the median element in the list, i. e. , the
middle number that divides the list into two halves of equal length so that
the median is greater or equal to all numbers preceding it, and lower or
equal to all numbers succeeding it. If the length of L is even, the function
median returns 0. It is up to you to choose the auxiliary parameters that
median takes. All auxiliary parameters must be numeric (not lists) and
should have initial values set to zero. For example, if L is ‘(1 2 3) and
you decide to use two additional auxiliary parameters, median should be
called as follows:
(median ‘(1 2 3) 0 0)
If there are three auxiliary parameters, then it must be called:
(median ‘( 1 2 3) 0 0 0 ) and so on.
Examples (for the sake of simplicity the auxiliary parameters are omitted;
in the actual calls they need to be set to zeros and placed right after the
list):
(median ‘(1)) returns 1
(median ‘(1 2)) returns 0
(median ‘(1 2 3)) returns 2
(median ‘( 1 2 3 4) returns 0
(median ‘( 3 3 3)) returns 3
(median ‘(1 4 7 10 11)) returns 7
(median ‘(2 4 4 10 10)) returns 4
(median ‘(10 13 14 15 20 20 40)) returns 15
(median ‘(20 24 25 31 45 56 70 80)) returns 0
(median ‘(15 22 25 30 36 42 48 60 70)) returns 36
The whole solution must be packed in one recursive function median which
must look as follows:
(define median (lambda (
initially set to zero>)
(cond
...
)))
In other words, you have to choose your auxiliary parameters and define a
COND statement.
Inside COND, you can use ONLY the following constructs:
- null?
- car
- cdr
- else
- =
- +
- median
- cond
- if
- user defined names (for your variables)
- integer literals
- parentheses
You cannot use a construct if it is not listed above. The use of built-in
functions is not allowed. You cannot define or call any other function with
the exception of median. In other words, your code must use only one
function, median, which must be defined using the constructs from the list
above.