Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / examples / finalizable / cons.c
... / ...
CommitLineData
1#include <stdio.h>
2
3typedef unsigned int uint;
4
5typedef struct Cons {
6 struct Cons *next;
7 int value;
8} *Cons;
9
10Cons listCons (int n, Cons c) {
11 Cons res;
12
13 res = (Cons) malloc (sizeof(*res));
14 fprintf (stderr, "0x%08x = listCons (%d)\n", (uint)res, n);
15 res->next = c;
16 res->value = n;
17 return res;
18}
19
20Cons listSing (int n) {
21 Cons res;
22
23 res = (Cons) malloc (sizeof(*res));
24 fprintf (stderr, "0x%08x = listSing (%d)\n", (uint)res, n);
25 res->next = NULL;
26 res->value = n;
27 return res;
28}
29
30void listFree (Cons p) {
31 fprintf (stderr, "listFree (0x%08x)\n", (uint)p);
32 free (p);
33}
34
35int listSum (Cons c) {
36 int res;
37
38 fprintf (stderr, "listSum\n");
39 res = 0;
40 for (; c != NULL; c = c->next)
41 res += c->value;
42 return res;
43}