[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: tree-depth

William James

11/7/2015 11:49:00 PM

Harald Hanche-Olsen wrote:

> + "Kaz Kylheku" <kkylheku@gmail.com>:
>
> | And then we can also do it in a readable way:
> |
> | (defun depth (tree)
> | (if (atom tree)
> | 0
> | (loop for node in tree
> | maximizing (depth node) into depth
> | finally (return (1+ depth)))))
>
> Nice. But I think I can improve even on this:
>
> (defun depth (tree)
> (if (atom tree)
> 0
> (1+ (loop for node in tree
> maximizing (depth node)))))

Gauche Scheme:

(use srfi-42 :only (max-ec))

(define (depth tree)
(if (pair? tree)
(+ 1 (max-ec (:list node tree) (depth node)))
0))

MatzLisp (Ruby):

def depth tree
if tree.is_a? Array
1 + tree.map{|x| depth x}.max
else
0
end
end

--
[Jesse Jackson] would spit into the food of white patrons he hated and then
smilingly serve it to them. He did this, he said, "because it gave me
psychological gratification." -- Life Magazine, 1969-11-29