[FRIAM] the role of metaphor in scientific thought

Marcus Daniels marcus at snoutfarm.com
Fri Jun 23 12:11:23 EDT 2017


< I'm going to skip ahead a bit and state that my entire line of rhetoric about circularity goes back to the complexity jargon discussion we were having and whether or not, as Nick put it, a system has a say in its own boundary.  It's all about _closure_.  This particular tangent targets closure from the functional programming perspective (or maybe from the procedural one, depending on how you look at it).  When you execute a loop in a "systems" language like C, you have a good chance that whatever you do in there could have side effects.  But when you do something like that in a purely functional language, you're very unlikely (never) going to leave side effects laying around. >

Incidentally, in GNU C one can have dirty closures.  Note how foo is assigned the value 2.  It changes the operation of the closure f .  While GNU C has function attributes for purity, they aren't enforced, they are only exploited for code generation.  Fortran has purity for more than just compiler guidance; a conforming compiler can enforce it.  And of course languages like Haskell tolerate none of this nonsense.   In languages like C and Fortran they aren't real closures, the lexical scope is only good for duration of the caller (here, main).   In functional languages, the context will remain entangled.  Characteristically, C++ gives the user the option to blow their head off and decide whether a closure (lambda) will copy its arguments.  

$ ./a.out 
 11 22 32 42 52
foo: 2

#include <stdlib.h>
#include <stdio.h>

int *map (int len, const int *a, int (*f)(int c))
{
   int i;

   int *ret = malloc (sizeof(a));

   for (i = 0; i < len; i++) {
     ret[i] = f(a[i]);
   }  
   return ret;
}


int
main()
{
   const int len = 5;
   const int a[] = { 10, 20, 30, 40, 50 };
   int foo = 1;

   int f(int c) {
     int val = c + foo;
     foo = 2;
     return val;
   }

   int *ret = map(len, a, f);
   int i;

   for (i = 0; i < len; i++) {
     printf(" %d", ret[i]);
   }
   printf("\n");
   printf ("foo: %d\n", foo);

  return 0;
}




More information about the Friam mailing list