同じシグネチャの関数ポインタを返す関数のシグネチャをtypedefしたい
Posted: 2015年7月18日(土) 06:35
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let a i = a
<interactive>:2:11:
Occurs check: cannot construct the infinite type: t1 ~ t -> t1
Relevant bindings include
i :: t (bound at <interactive>:2:7)
a :: t -> t1 (bound at <interactive>:2:5)
In the expression: a
In an equation for ‘a’: a i = a
Prelude>
#include <stdio.h>
#include <stdlib.h>
typedef void (*Handler(int))(int);
Handler* func0(int);
Handler* func2(int n)
{
printf("IN func2\n");
if (n <= 0) {
return NULL;
} else {
return (Handler *)func0;
}
}
Handler* func1(int n)
{
printf("IN func1\n");
if (n <= 0) {
return NULL;
} else {
return (Handler *)func2;
}
}
Handler* func0(int n)
{
printf("IN func0\n");
if (n <= 0) {
return NULL;
} else {
return (Handler *)func1;
}
}
int main(void)
{
int count = 10;
Handler *handler = (Handler *)func0;
while (handler != NULL) {
handler = (Handler *)handler(count--);
}
printf("count = %d\n", count);
return 0;
}
#include <stdio.h>
int count = 0;
typedef struct tgHANDLE Handle;
Handle func0(void);
struct tgHANDLE {
struct tgHANDLE (*func)(void);
};
typedef Handle (*Handler)(void);
Handle func2(void)
{
Handle func;
printf("IN func2\n");
if (count > 10) {
func.func = NULL; // NULLを返せば終了
} else {
++count;
func.func = func0; // 次の関数
}
return func;
}
Handle func1(void)
{
Handle func;
printf("IN func1\n");
if (count > 10) {
func.func = NULL; // NULLを返せば終了
} else {
++count;
func.func = func2; // 次の関数
}
return func;
}
Handle func0(void)
{
Handle func;
printf("IN func0\n");
if (count > 10) {
func.func = NULL; // NULLを返せば終了
} else {
++count;
func.func = func1; // 次の関数
}
return func;
}
int main(void)
{
Handler handler;
handler = func0; // 最初の関数のポインタ
while (handler != NULL) {
handler = handler().func;
}
printf("count = %d\n", count);
return 0;
}