[ create a new paste ] login | about

Link: http://codepad.org/Wxlpk3pp    [ raw code | output | fork | 1 comment ]

joshua_cheek - C, pasted on Nov 19:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int cube( int x )                 { printf("2mul %d\n",x); return x*x*x; }
int sum( int x , int y , int z ) { printf("2add\n"); return x+y+z; }

#define cube_NOE(x)    ({ printf("2mul %d\n",x); x*x*x; })
#define sum_NOE(x,y,z) ({ printf("2add\n"); x+y+z; })

int main(int argc, char **argv)
{
  printf("AOE:\n");
  printf( "solution = %d\n" , sum(cube(2),cube(3),cube(4)) );

  printf("\n\nNOE:\n");
  printf( "solution = %d\n" , sum_NOE(cube_NOE(2),cube_NOE(3),cube_NOE(4)) );

  return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
AOE:
2mul 4
2mul 3
2mul 2
2add
solution = 99


NOE:
2add
2mul 2
2mul 3
2mul 4
solution = 99


Create a new paste based on this one


Comments:
posted by joshua_cheek on Nov 19
Showing how normal order evaluation compares to applicative order evaluation.
reply