Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #53711

:Re: Still a Newbie - Existence in a list?

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject :Re: Still a Newbie - Existence in a list?
Date 2017-01-03 17:41 +0000
Organization Aioe.org NNTP Server
Message-ID <o4gnnn$pku$1@gioia.aioe.org> (permalink)

Show all headers | View raw


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))) ))

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

In Forth?


-- 
[T]he broadcast media ... create a separate and caustic virtual reality, then
broadcast that ideologically driven reality into the homes of millions of
people....  theoccidentalobserver.net/authors/Connelly-Gaza2.html

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

:Re: Still a Newbie - Existence in a list? "Robert L." <No_spamming@noWhere_7073.org> - 2017-01-03 17:41 +0000

csiph-web