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


Groups > comp.lang.forth > #53711 > unrolled thread

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

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2017-01-03 17:41 +0000
Last post2017-01-03 17:41 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.forth


Contents

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

#53711 — :Re: Still a Newbie - Existence in a list?

From"Robert L." <No_spamming@noWhere_7073.org>
Date2017-01-03 17:41 +0000
Subject:Re: Still a Newbie - Existence in a list?
Message-ID<o4gnnn$pku$1@gioia.aioe.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web