Name: Anonymous 2009-07-16 1:19
What Haskell has which can't be implemented in few lines of C?
Cfag status: told
-- This will not compile but I wanted to show you the type signature
getDataAndStuff :: IO (String, Int)
main = do
(name, age) <- getDataAndStuff
putStrLn ("omg " ++ name ++ ", its ur " ++ (show age) ++ "th bday")getDataAndStuff reads something from a database. Don't know, don't care. I'll use exactly the same function name for the C version. Also I could have made that string formatting a lot nicer but I couldn't be bothered.
#include <whatthefuck.h>
typedef struct tuple2_d6f {
char *fst;
int snd;
} tuple2_d6f;
tuple2_d6f getDataAndStuff();
int main(int argc, char **argv) {
tuple2_d6f temp = getDataAndStuff();
char *strtemp = "omg %s, its ur %dth bday";
printf(strtemp, temp.fst, temp.snd);
}/prog/? I went to the trouble of making a type that will be used only once. It'll have to be defined before getDataAndStuff(), which means it will probably be off in the wet shaggy depths of a header file somewhere, which means that if I have other two-tuples floating around (as the name "tuple2_d6f" might suggest) then I might make a gruesome typo and not catch it until I compile. ...Unless I use an IDE smart enough to notice this and suggest the correction right away, which is another question altogether.tuple_charpt_int isn't pretty, but at least it's less ambiguous. Hell, if my preprocessor is smart enough (not every C shop uses cpp, after all) maybe I can have a neat little macro that expands into the appropriate type. Or maybe I could use a higher-level language that compiles down to C, and then not have to worry about this.
typedef struct aids {
FUNCTION_POINTER fn;
void *closure;
} fake_lambda;
void *map(fake_lambda fn, void *list);
/* Now I can eat your liver! */
void *plus_one(void *closure, void *x) {
return (void *) ((int) x) + 1;
}
void get_work_done() {
fake_lambda f;
f.fn = &plus_one;
f.closure = NULL;
/* I don't really feel like finishing. */fake_lambda, which then gets passed to map(), which then probably uses a macro to call the fake_lambda so as to prevent users of this mechanism from fucking up...