由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请熟悉Scheme (LISP)编程的高手帮忙
相关主题
请教大牛和小牛帮助菜鸟ML编程问题!请大虾验证!
Lisp CAR CDR ...Sort a stack without using auxiliary stacks or arrays
char *p = "string literal"; 和 char a[] = "string liter (转载)$3.95的Lisp In Small Pieces
【申明】谁抹黑伟光正的java语言,谁就是人民的敌人。anyone using common lisp daily?
FP的可读性是最大问题重复利用threads的问题
Default function template argumentsanyone here using ruby?
ajax小问题如何GDB调试因pthread_cond_wait()阻塞的线程? (转载)
question about using Hive parameter (转载)有拿common lisp吃饭的没?
相关话题的讨论汇总
话题: median话题: returns话题: parameters话题: auxiliary话题: list
进入Programming版参与讨论
1 (共1页)
b**a
发帖数: 51
1
家庭作业,要求用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.
m*****n
发帖数: 204
2
So your problem is you can't call built in functions?
You can call recursively to find the length, and
Find the median during backtrack

the
that
function

【在 b**a 的大作中提到】
: 家庭作业,要求用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

b**a
发帖数: 51
3
Thanks for your tips. I can not use any built in function except those
provided in the description, CAR and CDR. I can use recursion to find the
length. Could you please tell me how to find the median during the backtrack
? Could you please provide some sample code? Your help would be greatly
appreciated. Homework is due tonight and very anxious now.
w******p
发帖数: 166
4
notice that "it is sorted in ascending order" -- you just need one
additional parameter to count where you are in the stepping, something like
s******d
发帖数: 363
5
a nonempty list L of positive integers (zero is not considered a positive
integer)
负值不在原来的list里,所以可以利用负的返回值来传递一点信息吧
w******p
发帖数: 166
6
go idea solnoid, this works with racket:
; m: counting length
; n: counting half of length
; p: n*2
; o: odd or even position
(define median
(lambda (xs m n p o)
(if (null? xs)
(if (= m p)
0
(- 0 n))
(let ((cb (median (cdr xs) (+ m 1)
(if (= o 0) (+ n 1) n)
(if (= o 1) (+ p 2) p)
(if (= o 0) 1 0))))
(if (= (- -1 m) cb)
(car xs)
cb)))))
> (median '(4 5 6) 0 0 0 0)
5
> (median '(4) 0 0 0 0)
4
> (median '(4 5 6 7 8) 0 0 0 0)
6
> (median '(4 5) 0 0 0 0)
0
> (median '(4 5 6 7) 0 0 0 0)
0
> (median '() 0 0 0 0)
0
1 (共1页)
进入Programming版参与讨论
相关主题
有拿common lisp吃饭的没?FP的可读性是最大问题
对pthread熟悉的XD请进来看看Default function template arguments
想学函数语言和高层次抽象的同学看过来ajax小问题
reverse LL recursivelyquestion about using Hive parameter (转载)
请教大牛和小牛帮助菜鸟ML编程问题!请大虾验证!
Lisp CAR CDR ...Sort a stack without using auxiliary stacks or arrays
char *p = "string literal"; 和 char a[] = "string liter (转载)$3.95的Lisp In Small Pieces
【申明】谁抹黑伟光正的java语言,谁就是人民的敌人。anyone using common lisp daily?
相关话题的讨论汇总
话题: median话题: returns话题: parameters话题: auxiliary话题: list