Question

In: Computer Science

Create a F# Function. Function Signature: zip L1 L2 The function zip combines the elements of...

Create a F# Function.
Function Signature: zip L1 L2

The function zip combines the elements of two lists pairwise, resulting in a list of tuples, where the first tuple in the list contains the first element of L1 as the first element, and the first element of L2 as the second element

// // zip L1 L2
// // Zip two lists
// // Returns list of tuples
// // Examples:
// zip [] [] => []
// zip [1] [1] => [(1, 1)]
// zip [1; 2; 40] [3; 56; 6] => [(1, 3); (2, 56); (40, 6)]
// zip [1; 2; 3] ['a'; 'b'; 'c'] => [(1, 'a'); (2, 'b'); (3, 'c')]

Solutions

Expert Solution

Code: if inbuilt zip function is not to be used and need to make a zip function

let rec zip(list1:'list1 list, list2:'list2 list) : list<'list1 * 'list2> =
    // stoping condition for recursive call
    if List.length list1 = 1 then [List.head list1 , List.head list2]
    // continue the recursive call if the length > 1
    else (List.head list1, List.head list2) :: zip (List.tail list1 , List.tail list2)
  

let list1 = [1; 2; 3]
let list2 = ['a'; 'b'; 'c']
// if both the list are empty
if list1.IsEmpty then
    let zippedlist = []
    printf "%A, %A => %A\n" list1 list2 zippedlist
//if there is some content  in the list
else 
    let zippedlist = zip(list1, list2)
    printf "%A, %A => %A\n" list1 list2 zippedlist

Try the code for different values of list1 and list2

Code;: if inbuilt zip function can be used

let zip list1 list2 = 
    List.zip list1 list2
  

let list1 = [1; 2; 3]
let list2 = ['a'; 'b'; 'c']
// if both the list are empty
if list1.IsEmpty then
    let zippedlist = []
    printf "%A, %A => %A\n" list1 list2 zippedlist
//if there is some content  in the list
else 
    let zippedlist = zip list1 list2
    printf "%A, %A => %A\n" list1 list2 zippedlist

Output:

Explaination:

Firstly check if the list has some contents or not if no content i.e if list.IsEmpty = true then we can print the empty list else we go to the zip function

Zip function is a recursive function which every time takes element at the Head position of both the list and insert it as a tuple in the resulting list


Related Solutions

1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal....
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal. Two sets are equal if they contain exactly the same members, ignoring ordering (or in other words, two sets are equal if they are a subset of each other). For example (set-equal? '(1 (2 3)) '((3 2) 1)) ---> #t (set-equal? '(1 2 3) '((3 2)1)) ---> #f (set-equal? '(1 2 3) '((1 2 3))) ---> #f 2. Two common operations on sets are...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify...
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify l2 (the nodes inserted in l1 should be copies of l2 class Node(object): def __init__(self, data): self.data = data self.next = None    class LinkedList(object): def print_list(self): cur_node = self.head while cur_node: print(cur_node.data) cur_node = cur_node.next def __init__(self): self.head = None;    def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last_node = self.head while last_node.next: last_node = last_node.next...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and L2. L1: ? = 7 + 2?, ? = 8 − 4?, ? = −9 + ? L2: ? = −8 − ?, ? = 3 − 3?, ? = 4 + 3? 2) Find polar form of complex number z : ?) ? = 4√3 − 4? ?) ? = 2√3 − 2i
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
designing a boiler in a chemical plant which is operated on twolevel L1 and L2 and...
designing a boiler in a chemical plant which is operated on twolevel L1 and L2 and two temeprature settings T1 and T2 ....L1: start inlet valve for fluid to flow in tank , L2 : stop inlet valve, T1 and T@ are the safe temperature range for some operation consider you as system engineer depending on follwing > promram development safe systes commiossioning fault finding sytem documentation What are the necessary action and feature you will add to task for...
. Observation data for the three L1, L2 and L3 locations are presented in the table...
. Observation data for the three L1, L2 and L3 locations are presented in the table as below L1 L2 L3 5 3 6 7 2 8 7 5 6 13 11 9 10 using α = 5%, test whether the conditions at the three locations are the same?
Recall that given a line L1 in the plane with slope m, a second line L2...
Recall that given a line L1 in the plane with slope m, a second line L2 is perpendicular to L1 if and only if the slope of L2 is − 1 m . Now consider the function h(x) = x^2 + 3/2x + 3. Among all of the lines tangent to the graph of h(x), there is exactly one which is perpendicular to the line given by y = −2x + 7. Write the equation of that line tangent to...
Give examples of languages L1 and L2 over {a, b} that satisfy the descriptions below: (a)...
Give examples of languages L1 and L2 over {a, b} that satisfy the descriptions below: (a) L1 is regular, L2 is nonregular, and L1 U L2 is regular; (b) L1 is regular, L2 is nonregular, and L1 U L2 is nonregular; (c) L1 is regular, L2 is nonregular, and L1 n L2 is regular; (d) L1 is nonregular, L2 is nonregular, and L1 U L2 is regular. (e) L1 is nonregular, L2 is nonregular, and L1 n L2 is regular.
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT