Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

da land of lisp

Name: Arthur 2012-05-31 20:00

weuhuhuhuhuhuhuhuhuhuhuhu

Name: THE SUSSMAN 2012-05-31 20:01


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    const char *s;
    struct node *next;
};

static void freelist(struct node *list)
{
    struct node *p;

    while (list) {
        p = list->next;
        free(list);
        list = p;
    }
}

static const char *car(const struct node *list)
{
    return list->s;
}

static struct node *cdr(const struct node *list)
{
    return list->next;
}

static struct node *append(struct node *head, struct node *tail)
{
    struct node *next;
    struct node *last;

    if (!head)
        return tail;
    for (next = head; (next = cdr(next)); last = next)
        ;
    last->next = tail;
    return head;
}

static struct node *cons(const char *s, struct node *list)
{
    struct node *node;

    if (!(node = malloc(sizeof *node))) {
        fprintf(stderr, "memory error\n");
        exit(EXIT_FAILURE);
    }
    node->s = s;
    node->next = list;
    return node;
}

static void print(const struct node *list)
{
    if (!list) {
        puts("0");
    } else {
        printf("%s -> ", car(list));
        print(cdr(list));
    }
}

struct leaf {
    const char *s;
    struct leaf *lesser;
    struct leaf *greater;
};

static void freetree(struct leaf *tree)
{
    if (!tree)
        return;
    freetree(tree->lesser);
    freetree(tree->greater);
    free(tree);
}

static struct node *treetolist(struct leaf *tree)
{
    struct node *s;

    if (!tree)
        return 0;
    s = cons(tree->s, treetolist(tree->greater));
    return append(treetolist(tree->lesser), s);
}

static void add(struct leaf **root, struct leaf *node)
{
    while (*root) {
        int t = strcmp((*root)->s, node->s);

        if (!t)
            return;
        root = t < 0 ? &(*root)->greater : &(*root)->lesser;
    }
    *root = node;
}

static struct leaf *maketree(const char **s, size_t size)
{
    struct leaf *root = 0;

    while (size--) {
        struct leaf *p = malloc(sizeof *p);

        if (!p) {
            fprintf(stderr, "memory error\n");
            exit(EXIT_FAILURE);
        }
        p->s = *s++;
        p->lesser = 0;
        p->greater = 0;
        add(&root, p);
    }
    return root;
}

int main(void)
{
    const char *s[] = { "hugh", "pugh", "barney mcgrew", "cuthbert",
                "dibble", "grub" };
    struct leaf *tree;
    struct node *list;

    tree = maketree(s, sizeof s / sizeof s[0]);
    list = treetolist(tree);
    freetree(tree);
    print(list);
    freelist(list);
    return 0;
}

Name: Anonymous 2012-05-31 23:24

You're only storing strings, silly Sussman

Name: Anonymous 2012-06-01 4:30

>>3
You can't spell Lambda Calculus without "C".

Name: Anonymous 2012-06-01 11:22


while(isLisp()){
    isShit();
}

Name: Anonymous 2012-06-02 13:05

>>4
Lambda Kalkulus.

Name: Anonymous 2012-06-02 16:25

lambda kababs

Name: Anonymous 2012-06-03 20:28

Lambder Arther Calculaterless

Don't change these.
Name: Email:
Entire Thread Thread List