Created
July 9, 2015 01:24
-
-
Save wangbj/cd6d972409b327444e3d to your computer and use it in GitHub Desktop.
After learnt some haskell..
This file contains 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
struct ListNode* uncons (struct ListNode* xxs, struct ListNode** xs) | |
{ | |
struct ListNode* x = xxs; | |
if (xxs) { | |
*xs = xxs -> next; | |
x -> next = NULL; | |
} | |
return x; | |
} | |
struct ListNode* cons (struct ListNode* x, struct ListNode* xs) | |
{ | |
x -> next = xs; | |
return x; | |
} | |
struct ListNode* concat (struct ListNode* xs, struct ListNode* ys) | |
{ | |
if (!xs) return ys; | |
struct ListNode* p = xs, *q = p->next; | |
while(q) { | |
p = q; | |
q = p->next; | |
} | |
p->next = ys; | |
return xs; | |
} | |
struct ListNode* singleton(struct ListNode* x) | |
{ | |
if (x) x -> next = NULL; | |
} | |
struct ListNode* reverse(struct ListNode* xxs) | |
{ | |
if (!xxs) return NULL; | |
struct ListNode* x, *xs; | |
x = uncons (xxs, &xs); | |
return concat (reverse(xs), singleton(x)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment