Answer the question
In order to leave comments, you need to log in
How to implement the map function?
Please help me understand the map function. What is she like? How to implement it and with what it is eaten in si? l=[a_1,..,a_n]
is some list of elements of type T, and f:T→T, then: map(f,l)↦[f(a_1 ),..,f(a_n )]
It is not clear what it does and how to implement it.
Answer the question
In order to leave comments, you need to log in
l=[a_1,..,a_n] is some list of elements of type T, and f:T→T, then:
map(f,l)↦[f(a_1 ),..,f(a_n )
] it does and how to implement it.
#include <stddef.h>
struct list {
struct list *next;
};
struct list *map(void f(struct list *p), struct list *l)
{
struct list *i;
for (i = l; i != NULL; i = i->next)
f(i);
return l;
}
struct list_int {
struct list list;
int i;
};
void process_int(struct list *l)
{
struct list_int *p = (struct list_int *)l;
++p->i;
}
int main()
{
struct list_int l[] = {
[0] = {
.list.next = &l[1].list,
.i = 1,
},
[1] = {
.i = 2,
},
};
map(process_int, &l[0].list);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question