From: Jung K Oh
Subject: Re: Looking for a Rexursive function to count a height of tree
Date:
Message-ID: <37n04a$kr8@news.uncc.edu>
Summary:
Keywords:
Is anybody out there to help me make a program that count a height of tree.
For example,
> (function '(a b c d))
0
> (function '(a (b c) d))
1
> (function '(a (b (c) d )))
2
> (function '((a b c) d))
nil ----> since not tree
From: Barry Margolin
Subject: Re: Looking for a Rexursive function to count a height of tree
Date:
Message-ID: <37plvn$1in@tools.near.net>
In article <··········@news.uncc.edu> ····@uncc.edu writes:
>Is anybody out there to help me make a program that count a height of tree.
I hope this isn't a homework assignment.
>For example,
>
>> (function '(a b c d))
> 0
I would call that a tree of height 1. An atom would be a tree of height 0.
>> (function '((a b c) d))
> nil ----> since not tree
It looks like a tree to me. In fact, *everything* in Lisp is a tree, since
an atom is just the degenerative case of a tree containing one node.
(defun tree-height (tree)
(if (atom tree) 0
(1+ (reduce #'max (mapcar #'tree-height tree)))))
--
Barry Margolin
BBN Internet Services Corp.
······@near.net
From: Simon Brooke
Subject: Re: Looking for a Rexursive function to count a height of tree
Date:
Message-ID: <CxuxGC.125@rheged.dircon.co.uk>
In article <··········@news.uncc.edu> ····@uncc.edu (Jung K Oh) writes:
Is anybody out there to help me make a program that count a height of tree.
For example,
> (function '(a b c d))
0
> (function '(a (b c) d))
1
> (function '(a (b (c) d )))
2
> (function '((a b c) d))
nil ----> since not tree
So many firstyear students seem to have been given this problem as
homework that I thought I'd post the solution to give us all some
peace. I do not intend to do anybody else's homework in future,
however.
(i) All your examples are trees. Any SExpr is a tree.
(2) The following is XLISP. You should check before submitting it as
homework to a course taught either in Common LISP or scheme.
(defun treeheight (form)
(cond ((null form) 0)
((atom form) 0)
(t (apply 'max (mapcar
'(lambda (elt)
(cond ((consp elt) (1+ (treeheight elt)))
(t 0))) form)))))
--
---------------
"There is no point in making further promises now about reducing
taxation: nobody believes us." Edward Heath, October 1994