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

C is also a functional programming language

Name: Anonymous 2013-11-18 10:44

Why not? I can just avoid side-effects. I can also treat functions as first-order data using Function pointers.

Name: Anonymous 2013-12-27 15:19

>>39
Please read your lambda calculi.

Name: Anonymous 2016-11-02 2:49

>>1
Most C programs can't be written with complete avoidance of side effects (I/O is essentially reading or writing a global variable and therefore a side effect). Complete avoidance of side effects means the program can only receive input and output through the OS shell, which makes several tasks outright impossible. For example, you can't write a cowsay or text adventure in a purely functional style.

And function pointers are quite awkward to use, and cannot be cast to generic pointers without causing undefined behavior. Additionally, I'm not aware of a portable way to achieve partial function application in C. (If there was, it would be possible to emulate the this pointer, and therefore do class-oriented programming).

>>8
A higher-order function is just a function that takes one or more functions as arguments, or returns a function. Both are possible in C; I used the former to write a definite integral evaluator which calculated the integral of an external unary function passed as a function pointer. Doing that without function pointers would require either placing the mathematical function within the body of the evaluation function, or implementing an expression parser. It's also possible to return a function pointer from a function, but this really only allows you to choose from a set of existing functions; constructing new functions at runtime would require an eval() function.

>>9
Again, that isn't portable.

>>12
That doesn't give you partial application, it just gives you a structure for storing arguments in advance before calling the function. The actual syntax for calling the function ends up being actually more verbose, you go from something like:

function(a,b,c);

to
struct closure a_closure;
a_closure.funcptr = &function;
a_closure.arg1 = a;
a_closure.arg2 = b;
a_closure.arg3 = c;
a_closure.funcptr(a_closure.arg1,a_closure.arg2,a_closure.arg3);


The verbosity makes it essentially pointless unless you're doing a lot of passing functions with pre-set arguments around.

Now in C++ you could probably achieve a cleaner closure syntax that makes partial function application easy, by using OOP and overloading the function call operator. So you could do something like

closure printIntegerWithNewLine(printf);
printIntegerWithNewLine.setArg(1, "%d\n");
printIntegerWithNewLine(51);


Which would ultimately equate to calling printf("%d\n", 51). Basically, the () operator would be overloaded to apply whatever arguments are already set, with any extra arguments taken from between the parentheses. A closure object with a N-ary argument and M arguments set could be called as a (N-M)-ary function. The same is of course true of your C closure, but the syntax is much more annoying.

Name: Anonymous 2016-11-02 4:52

>>1
I use the approach of macro-based symbolic composition in void.h,
a symbol can be a macro,string of code, a tuple or anything that can be parsed by C preprocessor as argument.
It looks a bit ugly with all repetition-emulating-recursion but its fairly fast and the code is much faster than any lithp

Name: Anonymous 2016-11-02 13:08

>>43
0.999... != 0.999...9

Name: Anonymous 2016-11-02 17:31

>>44
Proofs?

Name: Anonymous 2016-11-02 18:18

C is a pure functional programming language just like haskell, all code is just implicitly monadic

Name: Anonymous 2016-11-02 19:44

>>46
C isn't purely functional. The following code will have side effects in most implementations.
int a = 5;
scanf("%d", a);

Name: Anonymous 2016-11-03 1:32

My anus is purely functional

Name: Anonymous 2016-11-03 1:59

>>47
you didn't understand the joke

Name: Anonymous 2016-11-03 15:52

scanf("%d", a);

Ouch.

scanf("%d", &a);

was not what you meant?

Name: Anonymous 2016-11-03 16:28

>>50
Segmentation faults are a form of side effect.

Name: Anonymous 2016-11-03 18:12

>>47
C standard library isn't purely functional
FTFY

Name: Anonymous 2016-11-04 0:50

>>52
How would you make a useful program in C without the standard library?

Name: Anonymous 2016-11-04 1:25

>>53
By using another library for I/O, I assume.

Name: Anonymous 2016-11-04 1:48

Check my digits you fucking cunts

Name: Anonymous 2016-11-04 1:59

>>54
How would you implement that? C doesn't offer any I/O capabilities without the standard library.

Name: Anonymous 2016-11-04 5:18

Name: Anonymous 2016-11-04 5:23

>>56
Memory mapped stuff? Special inline ASM stuff?
After all, C doesn't provide e.g. graphics access, either.

Name: Anonymous 2016-11-04 6:06

shut up you fucking noobs

Name: xD !sDNR5LgF8Y 2016-11-04 6:24

>>59

le pedophile sage

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