Question

Two important functions that convert pairlists to pairs of lists and vice versa are Zip and...

Two important functions that convert pairlists to pairs of lists and vice versa are Zip and UnZip.

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].

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.

OZ programming language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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]

Answer:------------
a). The first solution refers to the case when the lists have the same length:
declare
fun {Zip Xs#Ys}
case Xs#Ys
of nil#nil then nil
[ ] (X|Xr)#(Y|Yr) then X#Y|{Zip Xr#Yr}
end
end
{Browse {Zip [a b c]#[1 2 3]}}
a).The second solution covers the cases when the lists may have also different
lengths:

declare
fun {Zip X}
case X of nil#nil then nil
[ ] X#nil then {Browse ’First list is too long’} X
[ ] nil#X then {Browse ’Second list is too long’} X
[ ] (H1|T1)#(H2|T2) then
H1#H2|{Zip T1#T2}
end
end
{Browse {Zip [a b c]#[1 2 3]}}
{Browse {Zip [a b c d]#[1 2 3]}}
{Browse {Zip [a b c]#[1 2 3 4]}}

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.

Answer:----
b). A condensed solution:
declare
fun {UnZip XYs}
case XYs
of nil then nil#nil
[ ] X#Y|XYr then
Xr#Yr={UnZip XYr}
in
(X|Xr)#(Y|Yr)
end
end
{Browse {UnZip [a#1 b#2 c#3]}}
b). An equivalent solution, where the code is explained in some details:
declare
fun {Unzip X}
case X
of nil then nil#nil
[] (H1#H2|T) then
Local Xr Yr %Xr and Yr are local variables
in
Xr#Yr={Unzip T} %when coming back from the recursion,
%Xr and Yr will be bound
(H1|Xr)#(H2|Yr) %construct the returned value
end
end
end
{Browse {Unzip [a#1 b#2 c#3]}}

Add a comment
Know the answer?
Add Answer to:
Two important functions that convert pairlists to pairs of lists and vice versa are Zip and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MATLAB - create MATLAB functions to convert kilometers to miles and vice versa according to the...

    MATLAB - create MATLAB functions to convert kilometers to miles and vice versa according to the following conditions. (a) The functions created will have input of miles or kilometers and the output with converted kilometers or miles. (b) The output of the function should be a conversion table. No input for the function is needed. (1 mile = 1 609.344 meter) - function with the following: (1) output a third-order polynomial function with the coefficients as the input variables; (2)...

  • 4. Convert the following from AR() to MA(-) or vice-versa. (a) X, = 1 +0.2X-1 +0.2X-2...

    4. Convert the following from AR() to MA(-) or vice-versa. (a) X, = 1 +0.2X-1 +0.2X-2 + w, (b) X, = 2 - 0.3-1 (c) X, = 2 - 0.3w1-1 -0.10.-2 + Wi. (d) X, = 3 -0.1%,-1 + W..

  • write the following functions in Picat, Haskell, or python using recursion. 1. member(x, lst): this function...

    write the following functions in Picat, Haskell, or python using recursion. 1. member(x, lst): this function checks to see if x occurs in lst. 2. sorted_list( lst): this function checks if lst is sorted in ascending order 3. Cartesian(xs, ys): this function takes two sets represented as lists, xs and us,, and returns the Cartesian product of the set, I.e a set of all possible pairs (x, y), where x is an element of xs, and y is an element...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

  • PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3...

    PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3 (10 pts) Define a function rev that takes a list xs and returns it in the reverse order. • F# standard library has List.rev, do not use it. You need to reimplement it. In [ ]: let rev (xs: 'a list) In [ ]: // Test you function List.rev [] = rev [] |> printfn "%" List.rev [1..9] = rev [1..9] |> printfn "%b'...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • 1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice...

    1 Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT