Search code examples
ozmozart

Scan through individual numbers in a list with Oz


I am working on learning Oz, but with very little online resources apart from the official documentation I am REALLY struggling to find out how to scan through a list in order to create a working partition function. In this example im just trying to return the first digit of the list. How would I do this?

declare

fun {Partition ?X}
   case X of nil then nil
   else
      {Show "HELLO!"}
      RETURN FIRST DIGIT OF X HERE?
   end
end

in
{Show {Partition [5 1 7 3 4 6 5]}}

Solution

  • You can use pattern matching, as a second case clause put:

    case X of nil then nil
    [] A|B then A    
    end
    

    X in this case equals to [5 1 7 3 4 6 5] and the only matching clause is the second and the simplest way is to assign 5 to A and [1 7 3 4 6 5] to B. This is just the beginning, now you can add more complex clause or use recursion in order to achieve other task more complex.