-
-
Save lucidfrontier45/3017b69a3bcabb6ac5227778abaa6bd4 to your computer and use it in GitHub Desktop.
Functional programming in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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