Monads/List monad: Difference between revisions

(Add Ocaml)
Line 141:
{{Out}}
<lang AppleScript>{{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}</lang>
 
=={{header|C}}==
 
The C type which best fits the concept used here for <code>monad</code> would be <code>void*</code>.
 
There's some limitations -- the list type characteristics in this context, if they had been used, would have required special attention to issues like traversing the list. And, C does not provide syntactic sugar which a user would likely expect from experience in some other languages.
 
Still, the task example is constrained enough that we can provide an implementation like:
 
<lang C>#include <stdio.h>
#include <stdlib.h>
 
#define MONAD void*
#define INTBIND(f, g, x) (f((int*)g(x)))
#define RETURN(type,x) &((type)*)(x)
 
MONAD boundInt(int *x) {
return (MONAD)(x);
}
 
MONAD boundInt2str(int *x) {
char buf[100];
char*str= malloc(1+sprintf(buf, "%d", *x));
sprintf(str, "%d", *x);
return (MONAD)(str);
}
 
void task(int y) {
char *z= INTBIND(boundInt2str, boundInt, &y);
printf("%s\n", z);
free(z);
}
 
int main() {
task(13);
}</lang>
 
Which, from the command line, might look like:
 
<lang bash>$ ./monad
13</lang>
 
 
=={{header|C++}}==
Line 272 ⟶ 314:
340, 357, 493
</pre>
 
 
=={{header|Clojure}}==
6,962

edits