Int List Monoid implementation in C

Here is an implementation in C of the monoid ([Int], ++, []):

/*
* Experimental fun. List Monoid implementation in C by Boro Sitnikovski
* 26.12.2013
*
*
boro@boro:~/test$ gcc list.c
boro@boro:~/test$ ./a.out
A = [ 1 2 3 ]
B = [ 5 6 7 ]
Head of list A: 1
Head of list B: 5
B ++ A = [ 5 6 1 2 3 ]
A ++ B = [ 1 2 5 6 7 ]
123:A ++ B = [ 123 1 2 5 6 7 ]
boro@boro:~/test$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let a = [1, 2, 3]
Prelude> let b = [5, 6, 7]
Prelude> head a
1
Prelude> head b
5
Prelude> b ++ a
[5,6,7,1,2,3]
Prelude> a ++ b
[1,2,3,5,6,7]
Prelude> 123:a ++ b
[123,1,2,3,5,6,7]
Prelude>
*
*/

#include
#include

#define T int
#define EMPTY_LIST NULL

typedef struct ListMonoid {
T value;
struct ListMonoid *next;
} ListMonoid;

ListMonoid *A, *B;

void list_init() {
static ListMonoid a1, a2, a3;
static ListMonoid b1, b2, b3;

a1.next = &a2; a2.next = &a3; a3.next = EMPTY_LIST;
b1.next = &b2; b2.next = &b3; b3.next = EMPTY_LIST;

a1.value = 1;
a2.value = 2;
a3.value = 3;

b1.value = 5;
b2.value = 6;
b3.value = 7;

A = &a1;
B = &b1;
}

T list_head(ListMonoid *L) {
return L->value;
}

void list_append(ListMonoid **L, T value) {
ListMonoid *newL = (ListMonoid *)malloc(sizeof(ListMonoid));
newL->value = value;
newL->next = *L;
*L = newL;
}

void list_print(ListMonoid *L) {
printf("[ ");
while (L) {
printf("%d ", L->value);
L = L->next;
}
printf("]\n");
}

void list_free(ListMonoid *L) {
ListMonoid *tmp = L;
while (tmp) {
L = tmp->next;
free(tmp);
tmp = L;
}
return;
}

ListMonoid *list_add(ListMonoid *L1, ListMonoid *L2) {
ListMonoid *tmp, *L;
tmp = L = (ListMonoid *)malloc(sizeof(ListMonoid));

while (1) { // L1
tmp->value = L1->value;
L1 = L1->next;
if (!L1) {
tmp->next = NULL;
break;
}
tmp = tmp->next = (ListMonoid *)malloc(sizeof(ListMonoid));
}

while (1) { // L2
tmp->value = L2->value;
L2 = L2->next;
if (!L2) {
tmp->next = NULL;
break;
}
tmp = tmp->next = (ListMonoid *)malloc(sizeof(ListMonoid));
}

tmp->next = NULL;

return L;

}

int main() {

ListMonoid *L;

list_init();

printf("A = ");
list_print(A);

printf("B = ");
list_print(B);

printf("Head of list A: %d\n", list_head(A));
printf("Head of list B: %d\n", list_head(B));

L = list_add(B, A);
printf("B ++ A = ");
list_print(L);
list_free(L);

L = list_add(A, B);
printf("A ++ B = ");
list_print(L);
list_free(L);

L = list_add(A, B);
list_append(&L, 123);
printf("123:A ++ B = ");
list_print(L);
list_free(L);


return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s