Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. List Operations Please define the following functions in Haskell. Please write your own code rather than using the list functions pre-defined by Haskell. (a)

image text in transcribed
image text in transcribed
3. List Operations Please define the following functions in Haskell. Please write your own code rather than using the list functions pre-defined by Haskell. (a) Function zip1 takes two lists as input and zips them together to produce a list of pairs. If the two list are not of the same length, the resultant list is of the length of the shorter one. Example: zipi (1,2,3,4) (2,3,4] [(1,2), (2,3), (3,4)] (b) Function zip2 takes two lists as input and zip them together to produce a list of pairs. If the two list are not of the same length, the resultant list is of the length of the longer one, and duplicate the elements in the longer list when the shorter one runs out. Example: zip2 (1,2,3,6) [2,3,4] [(1,2), (2,3), (3,4),(6,6)] (c) Function find takes an element and a list as input, and returns the index of the first och currence of the element in the list (the index of the first element is 0), and return -1 if the element is not in the list. Examples: find 5 (1,3,5,6,2,5,7) = 2 find 5 [1,2,4,7] = -1 (a) Function reduceList:: (a -> a -> a) -> [a] -> a takes two inputs, the first one is a function that takes two inputs and generate an output, all of type a, the second one is a list whose elements are of type a, and reduces the list using the function. Examples: reduceList (-) (8,3,1) - (8-3) -1 - 4 () Function compose takes two relations, represented as a list of pairs, as input, and computes the composition of the two relations. Example: compose ((1,2),(1,3)] [(2,3), (2,4).(3,2).(3,3)] - [(1,3).(1,4),(1,2)] 3. List Operations Please define the following functions in Haskell. Please write your own code rather than using the list functions pre-defined by Haskell. (a) Function zip1 takes two lists as input and zips them together to produce a list of pairs. If the two list are not of the same length, the resultant list is of the length of the shorter one. Example: zipi (1,2,3,4) (2,3,4] [(1,2), (2,3), (3,4)] (b) Function zip2 takes two lists as input and zip them together to produce a list of pairs. If the two list are not of the same length, the resultant list is of the length of the longer one, and duplicate the elements in the longer list when the shorter one runs out. Example: zip2 (1,2,3,6) [2,3,4] [(1,2), (2,3), (3,4),(6,6)] (c) Function find takes an element and a list as input, and returns the index of the first och currence of the element in the list (the index of the first element is 0), and return -1 if the element is not in the list. Examples: find 5 (1,3,5,6,2,5,7) = 2 find 5 [1,2,4,7] = -1 (a) Function reduceList:: (a -> a -> a) -> [a] -> a takes two inputs, the first one is a function that takes two inputs and generate an output, all of type a, the second one is a list whose elements are of type a, and reduces the list using the function. Examples: reduceList (-) (8,3,1) - (8-3) -1 - 4 () Function compose takes two relations, represented as a list of pairs, as input, and computes the composition of the two relations. Example: compose ((1,2),(1,3)] [(2,3), (2,4).(3,2).(3,3)] - [(1,3).(1,4),(1,2)]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

7. Prepare an effective outline

Answered: 1 week ago