blob: ba016fcfc2a8bc9831400bf5700bffb2a63a7cf8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/*
* Read numbers from stdin to a link list and print their sum to stdout.
* This is free and unencumbered software released into the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include "construct.h"
construct *readoubles()
{
double *x = malloc(sizeof(double));
if (scanf("%lf", x) != EOF)
return cons(x, readoubles());
free(x);
return NULL;
}
double sum(construct *list)
{
if (list == NULL)
return 0.0;
/* At program termination the memory will be freed anyway. */
return *(double *) car(list) + sum(cdr(list));
}
int main()
{
printf("%g\n", sum(readoubles()));
return 0;
}
|