L
Lee Xuzhang
/* from SICP -- Exercise 4.21:
((lambda (n)
((lambda (fact)
(fact fact n))
(lambda (ft k)
(if (= k 1)
1
(* k (ft ft (- k 1)))))))
10)
*/
/* I want to do the same thing like that in C/C++. */
#include <stdio.h>
/* I find that I can't declare the function what I want, in standard
C/C++.
* My colleague try the following code. It's OK, but I find something
wrong.
*/
int f( int (*g)(), int x )
{
if (x==0)
return 1;
else
return x * (*g)(g, x-1);
}
int h(void)
{
return 2;
}
int main()
{
printf("%d\n", f(f,5)); /* This is what I want. */
/* This is not I want; but the complier didn't warning. */
printf("%d\n", f(h,5));
return 0;
}
==========
/* I find a way to achieve it indirectly, but I do not like it. */
#include <stdio.h>
typedef struct scm {
int (*fp)(struct scm, int);
} SCM;
int f(SCM g, int x)
{
if (x==0)
return 1;
else
return x * (g.fp)(g, x-1);
}
int main()
{
SCM F;
F.fp = f;
printf("%d\n", f(F,5)); /* It's ugly.*/
return 0;
}
/* I am sorry for my poor english. I hope you can understand what I
want.
* I just want to try wrinting "continuation" in C/C++.
*
* This is my question:
* Can I do the same thing like this in standard C/C++?
* Can I passing a funtion pointer to a FUNTION which point to THE
FUNTION?
*/
((lambda (n)
((lambda (fact)
(fact fact n))
(lambda (ft k)
(if (= k 1)
1
(* k (ft ft (- k 1)))))))
10)
*/
/* I want to do the same thing like that in C/C++. */
#include <stdio.h>
/* I find that I can't declare the function what I want, in standard
C/C++.
* My colleague try the following code. It's OK, but I find something
wrong.
*/
int f( int (*g)(), int x )
{
if (x==0)
return 1;
else
return x * (*g)(g, x-1);
}
int h(void)
{
return 2;
}
int main()
{
printf("%d\n", f(f,5)); /* This is what I want. */
/* This is not I want; but the complier didn't warning. */
printf("%d\n", f(h,5));
return 0;
}
==========
/* I find a way to achieve it indirectly, but I do not like it. */
#include <stdio.h>
typedef struct scm {
int (*fp)(struct scm, int);
} SCM;
int f(SCM g, int x)
{
if (x==0)
return 1;
else
return x * (g.fp)(g, x-1);
}
int main()
{
SCM F;
F.fp = f;
printf("%d\n", f(F,5)); /* It's ugly.*/
return 0;
}
/* I am sorry for my poor english. I hope you can understand what I
want.
* I just want to try wrinting "continuation" in C/C++.
*
* This is my question:
* Can I do the same thing like this in standard C/C++?
* Can I passing a funtion pointer to a FUNTION which point to THE
FUNTION?
*/