[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Still a Newbie - Existence in a list?

William James

2/13/2016 6:10:00 PM

Thomas A. Russ wrote:

> > I could do (member 'b '(a b c)) but what about ...
> > '(b a (t y (q w (z c)) v) d) and I wanted to check ... v
>
> Of course, you could also write a simple find-in-tree function yourself.
> It shouldn't be too difficult. One example would be:
>
> (defun find-in-tree (item tree)
> ;; Return true if "item" can be found within "tree";
> ;; This assumes true lists, i.e., no dotted pairs.
> (or (eql item tree)
> (and (consp tree)
> (loop for clause in tree
> thereis (find-in-tree item clause))) ))

MatzLisp (Ruby):

def find_in_tree item, tree
tree.any?{|x| x == item or (x.is_a?(Array) and find_in_tree(item, x))}
end


find_in_tree 5, [2,[3,[4,5]]]
==>true
find_in_tree 52, [2,[3,[4,5]]]
==>false
find_in_tree [4,5], [2,[3,[4,5]]]
==>true
find_in_tree [3,[4,5]], [2,[3,[4,5]]]
==>true
find_in_tree 4, [2,[3,[4,5]]]
==>true

--
Goyim were born only to serve us. Without that, they have no place in the
world---only to serve the People of Israel.... Why are gentiles needed? They
will work, they will plow, they will reap. We will sit like an effendi and eat.
That is why gentiles were created. -- Rabbi Ovadia Yosef
https://web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx...