Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Haskell, there is a higher-order function unfold can be defined as follows to encapsulate a pattern of recursion for producing a list: unfold p

Using Haskell, there is a higher-order function unfold can be defined as follows to encapsulate a pattern of recursion for producing a list:

unfold p h t x | p x = [ ]

| otherwise = h x : unfold p h t (t x)

That is, the function unfold p h t produces the empty list if the predicate p is true of the argument, and otherwise produces a non-empty list by applying the function h to give the head, and the function t to generate another argument that is recursively processed in the same way to produce the tail of the list. For example, a function int2bin (to convert integers to binary numbers) can be written as follows:

int2bin = unfold (==0) (`mod`2) ('div`2)

[Note: putting function names mod and div inside back quotes allows them to be used infix]

Define the following functions using unfold:

a) map f

b) iterate f, where iterate f x produces a list by applying the function f to x an increasing number of times, as follows:

iterate f x = [x, f x, f (f x), f (f (f x)), ... ]

c) repHalve, where repHalve l takes list l, and returns a list of lists containing the elements of l, so that the first list contains half of ls elements (if l's length is odd, including the odd element), the next contains half of the remaining elements, and so on. For example, repHalve [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] should return [[1,2,3,4,5,6,7,8],[9,10,11,12],[13,14],[15]]

all above need code and simple test

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_2

Step: 3

blur-text-image_3

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

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions