0

Im writing a code for an exercise for my uni and have a problem. I'm quite beginner with Oz and just can't figure out why does this code not showing anything but is accepted by the compiler:

declare Tree W P T1 T2 T3 T4
fun {Count0 Tree}
   case Tree.subT of nil then
      if Tree.value==0 then
     1
      else
     0
      end
   [] H|T then
      if Tree.value==0 then 1+{Count0 T}
      else
     0+{Count0 T}
      end
   end
end
in
T1 = tree(value:0 subT:nil)
T2 = tree(value:0 subT:[T4])
T3 = tree(value:0 subT:nil)
T4 = tree(value:0 subT:nil)
T0 = tree(value:W subT:[T1 T2 T3])
{Browse {Count0 Tree}}

The code should count number of '0' in value of a tree and all of his subtrees that are in the list in tree.subT and in those trees and so on.

I'll be very grateful for any help!

wiwo
  • 721
  • 1
  • 13
  • 18

2 Answers2

0

First you never assign Tree neither W so the function will block in the first case statement. Then the recursive call {Count0 T} will bind the argument of the function Count0 to a list and not a tree since T is the rest of a list of trees contained in the subT field.

The Count0 function could take a list as argument. So to get the value field and to iterate recursively over the subtrees this function should be a little more sophisticated. You could use an accumulator for the count of the fields value containing 0 for example.

demesmaekerf
  • 146
  • 1
  • 6
0

I hope this is useful.

declare Tree T0 T1 T2 T3 T4 W=1
fun {Count0 Tree}
   case Tree.subT
   of nil then if Tree.value==0 then 1 else 0 end
   [] H|T then
      case T
      of nil then if Tree.value==0 then 1+{Count0 H} else {Count0 H} end
      else
         if Tree.value==0
         then 1+{Count0 H}+for I in T sum:S do {S {Count0 I}} end
         else {Count0 H}+for I in T sum:S do {S {Count0 I}} end
         end
      end
   end
end
in
T1 = tree(value:0 subT:nil)
T2 = tree(value:0 subT:[T4])
T3 = tree(value:0 subT:nil)
T4 = tree(value:0 subT:nil)
T0 = tree(value:W subT:[T1 T2 T3])
{Browse {Count0 T0}}
rbi
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 09 '22 at 18:41