Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
timick
Apr 7, 2016


205b posted:

code:
leaves :: [Node]
leaves = concat leafPairs
    where
        valuePairs = chunksOf 2 [1..2^k]                   -- I can trace this
        parentValues = map sum valuePairs                  -- and this
        parents = getParents $ zip parentValues leafPairs  -- but not this
        leafPairs = zipWith build parents valuePairs       -- or this
        build parent [valueA, valueB] = [Node valueA r, Node valueB r]
            where r = Leaf $ Parent parent


-- We're down to one parent, so make it a root
getParents :: [(Integer, [Node])] -> [Node]
getParents [(value, [childA, childB])] = [Node value $ Root children]
    where children = Children childA childB
The problem is the the recursive definition of leafPairs, which depends on parents which depends on leafPairs and so on.

Adbot
ADBOT LOVES YOU

timick
Apr 7, 2016


205b posted:

Is it a problem specifically because I'm trying to pattern match in getParents? Otherwise, I thought mutually dependent declarations were ok.
It has to do with the way that you've defined your tree. To create the parent node you need two child nodes, to create a child node you need a root node. Evaluating the tree will loop forever. Keep in mind that the parent field in a node is not a pointer or a reference to the parent node, it is a new copy of the parent node (which in turn will have new copies of child nodes...).

I'm not sure what you're after but would it be enough to have a leaf that looks like this instead?
code:
Leaf Integer
Where Integer would be the Integer value from the parent node.

timick
Apr 7, 2016


205b posted:

I did need a distinct value for each node, but it turned out I could get away with a singly-linked tree, so I went with that. Just trying to wrap my head around what I did wrong. I think I get it now! What you're saying is that let x = 0:y; y = 1:x in x doesn't give you two nodes pointing at each other, but a chain of infinitely many nodes.

As a followup, how come length (let x = 0:y; y = 1:x in x) pegs my processor but my original example just sat there doing nothing?

I hadn't noticed that it didn't consume any CPU in ghci, but I suspect it has to do with that the RTS can detect that something is wrong and stops executing. When I build and run your program it will fail with "project-exe: <<loop>>".

  • Locked thread