Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Does Haskell have a function that creates a list
      corresponding to the nodes in a tree, from the leaf node to the
      root node? (Markus L?ll)
   2. Re:  Does Haskell have a function that creates a  list
      corresponding to the nodes in a tree,     from the leaf node to the
      root node? (Thomas Davie)
   3. Re:  Does Haskell have a function that creates a list
      corresponding to the nodes in a tree, from the leaf node to the
      root node? (KC)
   4. Re:  Does Haskell have a function that creates a list
      corresponding to the nodes in a tree, from the leaf node to the
      root node? (Andy Elvey)
   5.  (no subject) (Fernando Henrique Sanches)
   6. Re:  [Haskell-cafe] (no subject) (Fernando Henrique Sanches)


----------------------------------------------------------------------

Message: 1
Date: Mon, 13 Jun 2011 01:17:49 +0300
From: Markus L?ll <[email protected]>
Subject: Re: [Haskell-beginners] Does Haskell have a function that
        creates a list corresponding to the nodes in a tree, from the leaf
        node to the root node?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Roger,

how that function works depends on what kind of tree you have. Without
parent-pointers it's essentially impossible to find the way back to
the root. If you have the root, and an instruction of how to get to
the leaf, then it's just a matter of prepending nodes to a list until
the instruction runs out.

But it actually *is* possible to efficiently have parent-pointers,
though it's not obvious when you come into to the language. The
technique is called tying the knot. What it basically means is to have
circular definitions, e.g having some expression bound to a name and
using that name in that same expression. Check it out:

data Tree a = Tree {
  { left  :: Maybe (Tree a)
  , right :: Maybe (Tree a) }

data PTree a = PTree
  { pLeft   :: Maybe (Tree a)
  , pRight  :: Maybe (Tree a)
  , pParent :: Maybe (Tree a) }

toP (Tree l r v) = let
     f :: Tree a -> TreeP -> TreeP
     f Nothing _ = Nothing
     f (Just (Tree l r)) parent = let
           self = TreeP (f l self) (f r self) (Just parent) -- tying
the knot here ..
        in self
     root = TreeP (f l root) (f r root) Nothing             -- .. and here
  in root

(The Maybes are there to mark lack of children in left and right
positions and lack of a parent in the parent position.)

Using this technique you can have all graphy data structures like
doubly linked lists and so on. The most simple knot tied is the black
hole:
x = x
, which takes forever to evaluate. And you can explicitly write graphs like:

data Node = Node Char [Node]

g = let
     a = Node 'A' [b, c]
     b = Node 'B' [c]
     c = Node 'C' [g, b, c]
  in Node 'G' [a]


--
Markus L?ll



------------------------------

Message: 2
Date: Mon, 13 Jun 2011 00:50:03 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Does Haskell have a function that
        creates a       list corresponding to the nodes in a tree,      from 
the leaf
        node to the root node?
To: Markus L?ll <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

The problem with knot tying exercises is that as soon as you try to update 
you're in trouble.

With an update in a tree with no parent pointers only the spine to get to the 
node being replaced in the tree need be recreated, all other parts of the tree 
can simply be referenced as they were before.  If you tie the knot on the other 
hand, every part of the tree must be recreated as soon as you update one node.

Bob

On 12 Jun 2011, at 23:17, Markus L?ll wrote:

> Hi Roger,
> 
> how that function works depends on what kind of tree you have. Without
> parent-pointers it's essentially impossible to find the way back to
> the root. If you have the root, and an instruction of how to get to
> the leaf, then it's just a matter of prepending nodes to a list until
> the instruction runs out.
> 
> But it actually *is* possible to efficiently have parent-pointers,
> though it's not obvious when you come into to the language. The
> technique is called tying the knot. What it basically means is to have
> circular definitions, e.g having some expression bound to a name and
> using that name in that same expression. Check it out:
> 
> data Tree a = Tree {
>  { left  :: Maybe (Tree a)
>  , right :: Maybe (Tree a) }
> 
> data PTree a = PTree
>  { pLeft   :: Maybe (Tree a)
>  , pRight  :: Maybe (Tree a)
>  , pParent :: Maybe (Tree a) }
> 
> toP (Tree l r v) = let
>     f :: Tree a -> TreeP -> TreeP
>     f Nothing _ = Nothing
>     f (Just (Tree l r)) parent = let
>           self = TreeP (f l self) (f r self) (Just parent) -- tying
> the knot here ..
>        in self
>     root = TreeP (f l root) (f r root) Nothing             -- .. and here
>  in root
> 
> (The Maybes are there to mark lack of children in left and right
> positions and lack of a parent in the parent position.)
> 
> Using this technique you can have all graphy data structures like
> doubly linked lists and so on. The most simple knot tied is the black
> hole:
> x = x
> , which takes forever to evaluate. And you can explicitly write graphs like:
> 
> data Node = Node Char [Node]
> 
> g = let
>     a = Node 'A' [b, c]
>     b = Node 'B' [c]
>     c = Node 'C' [g, b, c]
>  in Node 'G' [a]
> 
> 
> --
> Markus L?ll
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




------------------------------

Message: 3
Date: Sun, 12 Jun 2011 18:02:10 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] Does Haskell have a function that
        creates a list corresponding to the nodes in a tree, from the leaf
        node to the root node?
To: "Costello, Roger L." <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Roger:

It depends on what kind of tree you have and what kind of information
you expect to give the tree to find the leaf node.

If it's a binary search tree (BST) and you have the leaf key value
then use "find leafKey" and keep prepending the nodes you visit to the
list.

I might call the function: parents or ancestors. :)



On Sat, Jun 11, 2011 at 5:28 AM, Costello, Roger L. <[email protected]> wrote:
> Hi Folks,
>
> I am implementing the following function in another language. I want to see 
> if Haskell already has a function that does this; if so, I want to use the 
> Haskell function name rather than inventing my own.
>
> Here is the function that I am implementing: consider a tree data structure. 
> I pass to the function one of its leaf nodes. The function returns a list 
> containing that leaf node as the first element of the list, the leaf node's 
> parent as the second element of the list, ..., the tree's root node as the 
> n'th element of the list. (The list consists of all the nodes from the leaf 
> node to the root node)
>
> Does Haskell have a function that does this?
>
> If not, and you were to define a function for this, what name would you give 
> to the function?
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
--
Regards,
KC



------------------------------

Message: 4
Date: Mon, 13 Jun 2011 17:49:38 +1200
From: Andy Elvey <[email protected]>
Subject: Re: [Haskell-beginners] Does Haskell have a function that
        creates a list corresponding to the nodes in a tree, from the leaf
        node to the root node?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"


Hi all.....
This "tying the knot" looks interesting..... :)
The example that Markus gave immediately reminded me of a BNF grammar 
(which is very much a recursive-ish thing).
Does anyone know if "tying the knot" has been used in parsers?  Just 
curious..... it'd seem to be a useful thing to use in that area.
- Andy


On 13/06/11 13:02, KC wrote:
> Hi Roger:
>
> It depends on what kind of tree you have and what kind of information
> you expect to give the tree to find the leaf node.
>
> If it's a binary search tree (BST) and you have the leaf key value
> then use "find leafKey" and keep prepending the nodes you visit to the
> list.
>
> I might call the function: parents or ancestors. :)
>
>
>
> On Sat, Jun 11, 2011 at 5:28 AM, Costello, Roger L.<[email protected]>  
> wrote:
>> Hi Folks,
>>
>> I am implementing the following function in another language. I want to see 
>> if Haskell already has a function that does this; if so, I want to use the 
>> Haskell function name rather than inventing my own.
>>
>> Here is the function that I am implementing: consider a tree data structure. 
>> I pass to the function one of its leaf nodes. The function returns a list 
>> containing that leaf node as the first element of the list, the leaf node's 
>> parent as the second element of the list, ..., the tree's root node as the 
>> n'th element of the list. (The list consists of all the nodes from the leaf 
>> node to the root node)
>>
>> Does Haskell have a function that does this?
>>
>> If not, and you were to define a function for this, what name would you give 
>> to the function?
>>
>> /Roger
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110613/8f3fad8a/attachment-0001.htm>

------------------------------

Message: 5
Date: Mon, 13 Jun 2011 04:32:36 -0300
From: Fernando Henrique Sanches <[email protected]>
Subject: [Haskell-beginners] (no subject)
To: [email protected],
        [email protected],
        [email protected], [email protected],
        [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

http://maipenarai.com/lindex02.html



------------------------------

Message: 6
Date: Mon, 13 Jun 2011 06:36:44 -0300
From: Fernando Henrique Sanches <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] (no subject)
To: [email protected],
        [email protected],
        [email protected], [email protected],
        [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I'm sorry, somehow my e-mail account got kidnapped. The link is a virus and
should NOT be opened. I apologise for any inconvenience.

Fernando Henrique Sanches


2011/6/13 Fernando Henrique Sanches <[email protected]>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110613/15489b4c/attachment-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 36, Issue 30
*****************************************

Reply via email to