小编mar*_*ric的帖子

如何在 Haskell 中将函数作为参数传递给自身

在Python中,我可以写

\n
fac = lambda slf, n: 1 if n == 0 else n * slf(slf, n - 1)\nprint(fac(fac, 4))  # prints 24\n
Run Code Online (Sandbox Code Playgroud)\n

我正在尝试在 Haskell 中复制它。然而,Haskell 不会让我这样做:

\n
fac _ 0 = 1\nfac slf n = n * slf slf (n - 1)\n
Run Code Online (Sandbox Code Playgroud)\n

我收到此错误:

\n
    \xe2\x80\xa2 Occurs check: cannot construct the infinite type: t ~ t -> p -> p\n    \xe2\x80\xa2 In the first argument of \xe2\x80\x98slf\xe2\x80\x99, namely \xe2\x80\x98slf\xe2\x80\x99\n      In the second argument of \xe2\x80\x98(*)\xe2\x80\x99, namely \xe2\x80\x98slf slf …
Run Code Online (Sandbox Code Playgroud)

haskell

8
推荐指数
1
解决办法
379
查看次数

-finstrument-functions不适用于动态加载的g ++共享对象(.so)

这些天我在Ubuntu上用g ++共享对象(.so)文件测试-finstrument-functions.我发现了一个奇怪的行为 - 只有在静态链接库时,-finstrument-functions似乎才有效.如果我用dlopen/dlsym等链接到库,代码的功能仍然有效,但它不会调用__cyg_profile*函数.

以下是一些快速重现问题的代码:

MyLib.h

#ifndef __MYLIB_H__
#define __MYLIB_H__
class MyLib
{
public:
    void sayHello();
};
#endif
Run Code Online (Sandbox Code Playgroud)

MyLib.cpp

#include "MyLib.h"
#include <iostream>
using namespace std;

void MyLib::sayHello()
{
    cout<<"Hello"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

MyLibStub.cpp(.so的C接口)

#include "MyLib.h"

extern "C" void LoadMyLib ()
{
    MyLib().sayHello();
}
Run Code Online (Sandbox Code Playgroud)

Trace.cpp

#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
    void __cyg_profile_func_enter(void *this_fn, void *call_site)
        __attribute__((no_instrument_function));
    void __cyg_profile_func_exit(void *this_fn, void *call_site)
        __attribute__((no_instrument_function));
}
#endif

void __cyg_profile_func_enter(void* this_fn, void* call_site)
{
    printf("entering %p\n", (int*)this_fn);
}

void __cyg_profile_func_exit(void* this_fn, void* call_site)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ linux profiling g++ shared-libraries

7
推荐指数
1
解决办法
2048
查看次数

标签 统计

c++ ×1

g++ ×1

haskell ×1

linux ×1

profiling ×1

shared-libraries ×1