203k views
5 votes
Write the recursive version of the function map which takes

- m - a one-argument function that you want to map onto each element in the list.
- s - a sequence of values

User Larry Lu
by
8.1k points

1 Answer

3 votes

Final answer:

The student is asked to create a recursive function that maps a given function onto each element in a sequence. The recursive_map function applies the function to the first element and then recursively to the rest of the sequence, finally returning the new mapped list.

Step-by-step explanation:

The question is asking to write a recursive version of the map function. The map function applies a given function - m - to each item in a list - s - and returns a new list containing the results. Here is a simple recursive implementation in Python:

def recursive_map(m, s):
if not s:
return []
else:
return [m(s[0])] + recursive_map(m, s[1:])

This function checks if the list s is empty. If it is, it returns an empty list. Otherwise, it applies the function m to the first element of s, adds this to the result of recursively calling recursive_map on the rest of the list, and returns the resultant list.

User Jason Russell
by
8.2k points