小编NaC*_*aCl的帖子

标准ML中的y-combinator

我知道我可以像这样在SML中编写y-combinator:首先声明一个新的数据类型以绕过由于循环引起的类型不匹配.

datatype 'a mu = Roll of ('a mu -> 'a)
val unroll = fn Roll x => x
Run Code Online (Sandbox Code Playgroud)

现在您可以轻松定义y-combinator:

val Y = fn f => (fn x => fn a => f (unroll x x) a)
          (Roll (fn x => fn a => f (unroll x x) a)))
Run Code Online (Sandbox Code Playgroud)

然后你就完成了,你可以像这样使用它:

val f = Y (fn f => fn n => if n = 0 then 1 else n * f (n-1))
Run Code Online (Sandbox Code Playgroud)

我的问题是:是否有其他方法在SML中实现y-combinator?

functional-programming sml y-combinator

6
推荐指数
1
解决办法
280
查看次数

使用chrono将time_point转换为特定的持续时间

/*definition of start and end
std::chrono::time_point<std::chrono::system_clock> start;
std::chrono::time_point<std::chrono::system_clock> _end;
*/

std::chrono::time_point<std::chrono::system_clock> someclass::spf()
{
    _end = std::chrono::system_clock::now();
    std::chrono::time_point<std::chrono::system_clock> time(_end-start);
    start = std::chrono::system_clock::now();
    return time;
}
unsigned int someclass::secs()
{
    return std::chrono::duration_cast<std::chrono::seconds>(spf()).count();
}
Run Code Online (Sandbox Code Playgroud)

编译器给我的错误调用duration_cast.确切的错误:

error: no matching function for call to ‘duration_cast(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >)’
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
1
解决办法
5836
查看次数

C++复制流操纵器到其他流

想象一下,std::ostream& operator<<想要用数字来做一些事情.为此目的,有人可能想要使用std::hex,其他人可能想要使用none,无论如何,任何操纵器都是可能的.

如果std::ostream没有ostream传递参数的文本内容,我怎么能把它们复制到另一个?我需要操纵器.

所以我想要那样std::cout << std::hex << someCoolClass(10),在哪里someCoolClass看起来像

struct someCoolClass
{
    someCoolClass(int i) : _i(i)
    {}

    friend std::ostream& operator<<(std::ostream& os, const someCoolClass& rhs)
    {
        std::stringstream ss;
        //magically copy manipulators of os
        ss << _i;
        return os << ss.str();
    }
private:
    int _i;
};
Run Code Online (Sandbox Code Playgroud)

打印a.我知道这个例子是无用的,特别是将整数转换为字符串的其他流似乎没用,但让我们想象一下这不是无用而不是纯粹的非法.

谢谢.

c++ stl stream c++11

4
推荐指数
1
解决办法
191
查看次数

在某个谓词下在编译时填充数组

有没有办法制作一个constexpr无符号整数的循环,它满足constexpr布尔函数给出的一些谓词pred(std::size_t)

我尝试了很多,特别是使用索引技巧,只是为了发现我的数据太大了,这样它超过了递归模板实例化限制256.我将无法改变这个限制,如果可能的话更改.

正如评论中所提到的,这里有一些我想要实现的伪代码:

template<std::size_t... Is> 
struct Sequence{};

template<std::size_t N, std::size_t... Is> 
struct SequenceGenerator : SequenceGenerator<N-1, N-1, Is...>
{}; //obviously here it gets too deep into recursion, as mentioned

template<std::size_t... Is> 
struct SequenceGenerator<0, Is...> : Sequence<Is...>
{};

template<std::size_t N>
struct MyData
{
    std::size_t values[N];

    static constexpr std::size_t size()
    { return N; }
};

template<typename Lambda, std::size_t... Is> 
constexpr MyData<sizeof...(Is)> MyGen(Sequence<Is...>, Lambda func)
{
    if(func(Is)...)
        return {{ Is... }};
    else
        return /*some not-invalidating but current element …
Run Code Online (Sandbox Code Playgroud)

c++ compile-time template-meta-programming variadic-templates c++11

4
推荐指数
1
解决办法
238
查看次数

C++向量的所有组合

让我们说我们有一些数字0 to n,我们想要争夺那些规模,s并希望看到每一种可能的组合.

所以排列的数量恰好等于s! * n!/(s!*(n-s)!).

n = 3和的示例s = 3:

0 1 2 | 0 1 3 | 0 2 1 | 0 2 3 | 0 3 1 | 0 3 2 | 1 0 2 | 1 0 3 | 1 3 2 
1 2 3 | 1 2 0 | 1 3 0 | 2 0 1 | 2 1 0 | 2 0 3 | …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm boost stl c++11

2
推荐指数
2
解决办法
3089
查看次数

工具提示中的 Qt 小部件

是不是,也许通过使用一些肮脏的哈克什东西,可以将 aQWidget作为工具提示或存档类似的机制?我想要这样的东西,因为我有一个竞赛的一些参与者的列表,并且QListView当我悬停在总参与者数上时,我想使用 a来显示该竞赛中的每个人。我正在使用Qt5.3.

c++ user-interface qt c++11 qt5.3

2
推荐指数
1
解决办法
3565
查看次数

卡西欧的模数fx991es

我最近在stackoverflow上找到了一种使用fx991es计算模数的方法.它必须配置为使用辐射才能使其工作.

Pol(-Rec(1/(2?) , 2?×A/B), Y)(? - Y)B
Run Code Online (Sandbox Code Playgroud)

如何在Casio fx-991ES计算器中计算Mod b

但是,我真的不明白这是如何工作的.我看到它的一些数学黑客使用极坐标和东西.但为什么这实际上有效呢?

math calculator

0
推荐指数
1
解决办法
1005
查看次数