0

I'm new to mozart oz and I have this problems to solve:

  a) Implement a function Zip that takes a pair Xs#Ys of two lists Xs and Ys (of the
 same length) and returns a pairlist, where the first field of each pair is taken
 from Xs and the second from Ys. For example, {Zip [a b c]#[1 2 3]} returns the
 pairlist [a#1 b#2 c#3].

  b) The function UnZip does the inverse, for example {UnZip [a#1 b#2 c#3]}
 returns [a b c]#[1 2 3]. Give a specification and implementation of UnZip.

All I know is that # is a label of some sort which composes a pair/tuple, taken from the docs, but I couldn't find an example that illustrates how to work with it.

My question is how to split it to get the items or how to work with that label (or any source that might have an example of how to work with it).

I've done a little bit of search and I arrived at this piece of code (I don't know if it's even correct syntactically):


    declare
    fun {Zip L}
       case L of nil then nil
       [] L1#L2 then .... % Here i'm stuck or i don't know how to proceed
       end
    end

    {Browse {Zip ['a' 'b' 'c']#[1 2 3]}}

Any help will be appreciated.

Thanks for your time.

Cipri
  • 15
  • 1
  • 6

2 Answers2

1

Basically, pattern matching in Oz is one of the most powerful tools. We can't use for-loops, as varibles are unmutable, so we use recursion.

functor
import
    System
    Application
define
    fun {Zip L}
      case L of L1#L2 then
        case L1 of H1|R1 then
          case L2 of H2|R2 then
            H1#H2|{Zip R1#R2}
          else nil end
        else nil end
      else nil end
    end
    {System.show {Zip ['a' 'b' 'c']#[1 2 3]}} 

    % Shows [a#1 b#2 c#3]

    {Application.exit 0}
end
  • 1
    How does this code solve the issue described? Please elaborate – Orestis Zekai Nov 27 '19 at 13:52
  • thank you, the first requirement it's solved, but now i'm stuck at UnZip function. Can you provide a tip of how to solve it? Thx :) – Cipri Dec 08 '19 at 09:20
  • I managed to do this ```declare fun {UnZip L S F} case L of nil then S#F [] H|T then case H of H1#H2 then {UnZip T H1|S H2|F} else nil end end end {Browse {UnZip [a#1 b#2 c#3] nil nil}} % Shows [c b a]#[3 2 1] % Should show [a b c]#[1 2 3]``` but it returns the lists in the reverse order, any suggestion to make it show the correctly? – Cipri Dec 08 '19 at 09:46
1

All you need is a function that reverses the lists:

    declare
    fun {ReverseAux L1 L2}
       case L1
       of nil then L2
       [] H|T then {ReverseAux T H|L2}
       end
    end
    
    
    % wrapper for reverse
    
    declare
    fun {Reverse L}
       {ReverseAux L nil}
    end
    
    

Then, your UnZip function can be written like this:

    declare
    fun {UnZipAux L S D}
       case L
       of nil then {Reverse S}#{Reverse D}
       [] X#Y|T then {UnZipAux T X|S Y|D}
       end
    end

    
    % wrapper for UnZip

    declare
    fun {UnZip L}
       {UnZipAux L nil nil}
    end

Hope you did well bro, this is currently my homework as well :))

Arrriba
  • 41
  • 1
  • 6