(I understand that C is not intended to be used in a functional way. However, having learned functional programming, I have trouble thinking differently.)
Given those restrictions:
- No nested functions because clang forbids it (so no lamda expressions)
- no global variables
Can we think of a way of lifting a function f to take an extra parameter to fit a prototype, such as this new parameter will be ignored when f is executed?
Here is in detail what I want to do:
I want to fit a function f whose type is:
f :: void f(char *s)
in a function g that takes an function as an argument (call it arg), whose type is:
arg :: void f(unsigned int i, char *s)
Thus, the type of g is:
g :: void g(void (*f) (unsigned int, char))
The solution in haskell would be the following:
g (const f)
Is that even possible, maybe with some kind of macro wizardry?
EDIT: To provide a better understanding, here is the actual code. The body of ft_striter needs to be completed. The purpose is: apply the function f to every character of a string, using or not using its index i.
void ft_striter(char *s, void (*f) (char *s))
{
ft_striteri(?);
}
static void ft_striteri_helper(char *s, unsigned int i, void (*f)(unsigned int, char*))
{
if (*s)
{
f(i, s);
ft_striteri_helper(s + 1, i + 1, f);
}
}
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
ft_striteri_helper(s, 0, f);
}