Skip to content

Instantly share code, notes, and snippets.

@lucidfrontier45
Created October 17, 2019 13:55
Show Gist options
  • Select an option

  • Save lucidfrontier45/3017b69a3bcabb6ac5227778abaa6bd4 to your computer and use it in GitHub Desktop.

Select an option

Save lucidfrontier45/3017b69a3bcabb6ac5227778abaa6bd4 to your computer and use it in GitHub Desktop.
Functional programming in C
#include <stdio.h>
double sum(const double *x, double s, size_t n)
{
if (n == 0)
{
return s;
}
return sum(x + 1, s + *x, n - 1);
}
double mean(const double *x, size_t n)
{
double s = sum(x, 0.0, n);
double m = s / n;
return m;
}
int main(int argc, char **argv)
{
size_t n = 5;
double x[] = {1, 2, 3, 4, 5};
double m = mean(x, n);
printf("m = %f\n", m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment