Question
code: javascript/rust /* TASK 2 (10pts): A good way to think about fold is that it _folds_ a list into a single value. This single
code: javascript/rust
/* TASK 2 (10pts):
A good way to think about fold is that it _folds_ a list into a single value.
This single value can, of course, also be anything -- even a list!
To emphasize this, your next task is to write an implementation of the map
function we saw in class using fold_left.
map takes in 2 arguments:
1. g - A function that is to be mapped over a list.
2. ls - The list that is to be mapped over.
Here's an example of how map is supposed to work:
map(x => x + 1, List([1, 2, 3])) ------> List([2,3,4])
map(x => x * 2, List([1, 2, 3])) ------> List([2,4,6])
You can normally do this using a loop construct, but your constraint here is
to define map using fold_left only.
Hint: the base value could be an empty list (List([])).
*/
const map = (g, ls) => /**
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started