求助。common lisp问题。新手。

刘昊源 发布于 2013/05/27 19:40
阅读 412
收藏 0
Write the following functions using recursion:
(last-element list) returns the last element of list;
(all-but-last list) returns all but the last element of list;
(middle list) returns the middle element of list,
assuming an odd number of elements.

Don't use any numbers to implement “middle.” You will need to make use of "cdr" and "all-but-last" for the definition of “middle.”


这个不用数字应该怎么解决?求助大神。

加载中
0
crab2313
crab2313
(defun last-element (list)
  (let ((rest (cdr list)))
    (if (null rest)
	(car list)
	(last-element rest))))

(defun all-but-last (list)
  (labels ((all-but-last-r (list result)
	     (let ((rest (cdr list)))
	       (if (null rest)
		   result
		   (all-but-last-r rest (append result (list (car list))))))))
  (all-but-last-r list '())))

(defun middle (list)
  (let ((rest (cdr list)))
    (if (null rest)
	list
	(middle (all-but-last rest)))))
crab2313
crab2313
作业应该自己写
0
accesstolaw
accesstolaw
该评论暂时无法显示,详情咨询 QQ 群:点此入群
0
accesstolaw
accesstolaw

没看懂这个是神马语言?

c++ 还是 c#? 或者其他的

crab2313
crab2313
该评论暂时无法显示,详情咨询 QQ 群:点此入群
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部