这就是我的深度程序,但是如何在不使用max函数的情况下做到这一点(只使用定义、lambda、引号(‘)、car、cdr、cons、cond、eq?和equal?)?
(define depth
(lambda (expr)
(cond ((null? expr) 0)
((list? (car expr))
(max (+ 1 (depth (car expr))) (depth (cdr expr))))
((null? (cdr expr))0) (max (depth (cdr expr))))))输入:((id = id + id)(if bool then (if bool then (id = id + id ))(id = const / const)(id = id + id))(while bool ( id = id - const)(id = id -id)
应输出:最大深度:2
发布于 2012-12-03 11:39:16
当然,您也可以实现自己的my-max,并使用它来代替内置的max过程:
(define (my-max a b)
(if (> a b) a b))为了找到最大深度,你必须以某种方式进行基本相同的比较--所以可以将其重构为一个辅助过程。请注意,内联比较并不是一个好主意,因为这将需要计算两次递归调用-最好坚持使用助手过程,无论是max还是my-max。
此外,在您的代码中对max的第二次调用是不必要的-如果只有一个值,为什么需要查找最大值?
https://stackoverflow.com/questions/13676784
复制相似问题