Name: Anonymous 2012-11-20 23:17
post code in this thread and it will be rewritten in c.
#|㼲ℒv "." + ()import Control.Applicative
add :: (Applicative f, Num a) => f a -> f a -> f a
add = (<*>).((+)<$>)
> add (pure 1) (pure 2)
3add(1, 2) == 3> add (Just 1) Nothing
Nothingint x = 1;
add(&x, NULL) == NULL> add [1,2,3] [10,20,30]
[11,21,31,12,22,32,13,23,33]int *xs = { 1, 2, 3 }, *ys = { 10, 20, 30 };
add(xs, ys) == { 11, 21, 31, 12, 22, 32, 13, 23, 33 }> let m = putStr "? " >> readLn in add m m
? 1
? 2
3int f() {
int x;
printf("? ");
scanf("%d", &x);
return x;
}
add(&f, &f) == 3 /* if the user types in 1 and 2 */> add (+1) (*2) 3
10int f(int x) {
return x + 1;
}
int g(int x) {
return x * 2;
}
int (*result)(int);
result = add(&f, &g);
result(3) == 10
int add1(int a, int b) {
return a + b;
}
int add2(int a, int a_ok, int b, int b_ok, int* c) {
if(a_ok && b_ok) {
*c = a + b;
return 1;
}
return 0;
}
void add3(int* a, int a_len, int* b, int b_len, int* c) {
int i1, i2, i3;
i3 = 0;
for(i1 = 0; i1 < b_len; i1++) {
for(i2 = 0; i2 < a_len; i2++, i3++) {
c[i3] = a[i2] + b[i1];
}
}
}
int add4(int (*f)(int), int (*g)(int), int x) {
return f(x) + g(x);
}