我如何在 Rust 中模拟 Lisp(应用)或(咖喱)? [英] How do I emulate Lisp (apply) or (curry) in Rust?

查看:53
本文介绍了我如何在 Rust 中模拟 Lisp(应用)或(咖喱)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 QuickCheck 移植到 Rust,除了 for_all 之外,我已经编写了所有内容 因为我不确定类型签名应该是什么.

I'm porting QuickCheck to Rust, and I've written everything except for_all as I'm not sure what the type signature should be.

我知道一般来说,for_all 会接受一个属性 lambda 和一组生成器 lambda.它将评估生成器以创建一个随机测试用例,将属性作为输入.

I know that in general, for_all will accept a property lambda and a collection of generator lambdas. It will evaluate the generators in order to create a random test case to give the property as input.

应该打印+++ OK,通过100次测试.如果属性返回true,否则应该打印*** Failed!并打印有问题的测试案例值.

It should print +++ OK, passed 100 tests. if the property returns true, otherwise, it should print *** Failed! and print the offending test case values.

推荐答案

在 Rust 中,所有函数都采用固定数量的参数,因此一般来说没有与 Lisp 的 apply 等效的东西case,但是宏可以为您提供您想要的抽象.你可以写:

In Rust, all functions take a fixed number of parameters, so there's no such thing as an equivalent to Lisp's apply in the general case, but macros can provide you with the abstraction you desire. You can write:

macro_rules! for_all {
    ( $tester:expr, $( $generator:expr ),* ) => {
        $tester( $($generator() ),* )
    }
}

然后,for_all!(|a, b| a + b, || 4, || 7) 产生 11.

祝你的项目好运!

这篇关于我如何在 Rust 中模拟 Lisp(应用)或(咖喱)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆