2

Can somebody help me in translating this piece of Python code to Oz language?

def rep_subset_conditional(S, tset, index, t, count):
    for i in xrange(index, len(S)):
        tset += [S[i]]
        tsum = sum(tset)
        if tsum == t:
            print tset
            count += 1
            tset.remove(S[i])
            return count
        elif tsum > t:
            tset.remove(S[i])
            return count
        else:
            count=rep_subset_conditional(S, tset, i, t, count)
            tset.remove(S[i])
    return count

This code simply counts and prints all the subsets(with repetition of elements) of a given set whose sum equals t. Below is a trial run of this code:

>>> rep_subset_conditional([1, 5, 10, 25, 50], [], 0, 10, 0)
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    [1, 1, 1, 1, 1, 5]
    [5, 5]
    [10]
    4

t here was 10 S was subset [1, 5, 10, 25, 50] I want to translate this program into Oz. But I'm unable to do that correctly. Please help! This is what all I tried:

declare Rep T1 T2 Sum
fun {Rep L S MainList AuxList C}
   case L of nil then C
   [] H|T then
      {Browse H|AuxList}
      if {Sum H|AuxList} == S then
      T1={Rep MainList S MainList H|AuxList C+1}
      {Rep T S MainList AuxList T1}
      else
      if {Sum H|AuxList} < S then
         T2 = {Rep MainList S MainList AuxList C}
         {Rep T S MainList AuxList T2}
      end
      end      
   end
end
fun {Sum L}
  case L of nil then 0
  [] H|T then
     H + {Sum T}
  end
end
Ilario Pierbattista
  • 3,175
  • 2
  • 31
  • 41
batman
  • 703
  • 1
  • 9
  • 27
  • What did you try? What isn't working? Be more specific! – nneonneo Aug 24 '12 at 04:12
  • Well, I am new to Oz(and functional programming in general) I posted all what i tried. I could not manage to do recursion with looping both at same time in Oz(without using `for` construct of Oz). – batman Aug 24 '12 at 04:19
  • 1
    You are not handling the case `{Sum H|AuxList} > S`, so in this case the `Rep` function does not know what to return. – wmeyer Aug 24 '12 at 17:07

0 Answers0