编写Scheme数据结构,以便可以对它们进行eval-d返回,或者替代

发布于 2024-08-22 10:25:28 字数 1179 浏览 3 评论 0原文

我正在 PLT 方案中编写一个应用程序(杂耍图案动画师)接受Scheme表达式作为某些字段的值。我正在尝试编写一个小型文本编辑器,它让我将表达式“分解”为仍然可以评估但包含文本数据的表达式以进行手动调整。

例如,

(4hss->sexp "747")

是生成合法模式的函数调用。如果我评估并打印它,它就

(((7 3) - - -) (- - (4 2) -) (- (7 2) - -) (- - - (7 1)) ((4 0) - - -) (- - (7 0) -) (- (7 2) - -) (- - - (4 3)) ((7 3) - - -) (- - (7 0) -) (- (4 1) - -) (- - - (7 1)))

可以作为字符串“读取”,但不会像函数一样“评估”。当然,对于这个陈述,我需要的很简单,

(quote (((7 3...

但其他例子并不简单。例如,这个包含打印为向量的结构:

pair-of-jugglers
; -->
(#(struct:hand #(struct:position -0.35 2.0 1.0) #(struct:position -0.6 2.05 1.1) 1.832595714594046) #(struct:hand #(struct:position 0.35 2.0 1.0) #(struct:position 0.6 2.0500000000000003 1.1) 1.308996938995747) #(struct:hand #(struct:position 0.35 -2.0 1.0) #(struct:position 0.6 -2.05 1.1) -1.3089969389957472) #(struct:hand #(struct:position -0.35 -2.0 1.0) #(struct:position -0.6 -2.05 1.1) -1.8325957145940461))

我想到了至少三种可能的解决方案,但我不太喜欢其中任何一个。

在我再次开始做糟糕的递归梦之前帮助我吧。

I'm writing an application (A juggling pattern animator) in PLT Scheme that accepts Scheme expressions as values for some fields. I'm attempting to write a small text editor that will let me "explode" expressions into expressions that can still be eval'd but contain the data as literals for manual tweaking.

For example,

(4hss->sexp "747")

is a function call that generates a legitimate pattern. If I eval and print that, it becomes

(((7 3) - - -) (- - (4 2) -) (- (7 2) - -) (- - - (7 1)) ((4 0) - - -) (- - (7 0) -) (- (7 2) - -) (- - - (4 3)) ((7 3) - - -) (- - (7 0) -) (- (4 1) - -) (- - - (7 1)))

which can be "read" as a string, but will not "eval" the same as the function. For this statement, of course, what I need would be as simple as

(quote (((7 3...

but other examples are non-trivial. This one, for example, contains structs which print as vectors:

pair-of-jugglers
; -->
(#(struct:hand #(struct:position -0.35 2.0 1.0) #(struct:position -0.6 2.05 1.1) 1.832595714594046) #(struct:hand #(struct:position 0.35 2.0 1.0) #(struct:position 0.6 2.0500000000000003 1.1) 1.308996938995747) #(struct:hand #(struct:position 0.35 -2.0 1.0) #(struct:position 0.6 -2.05 1.1) -1.3089969389957472) #(struct:hand #(struct:position -0.35 -2.0 1.0) #(struct:position -0.6 -2.05 1.1) -1.8325957145940461))

I've thought of at least three possible solutions, none of which I like very much.

Help me out before I start having bad recursion dreams again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-08-29 10:25:28

我不确定你想做什么。具体来说,尝试生成具有可 eval 的代码的文件似乎是一个奇怪的选择。无论如何,当您处理结构时,创建随机值的序列化都是一个问题——因为可能存在具有相同名称的不同结构。一些可能与您相关的要点:

  • 有一个未记录的库scheme/fasl,它可以以二进制格式读取和写入值(对于大数据有用)。

  • 还有 mzlib/pconvert —— DrScheme 用来将值打印为可计算表达式的库(但这不适用于所有类型的数据)。

  • 如果你想使用很容易以可读形式编写的结构,那么你可以使用“预制”结构。

(有关更多详细信息,最好在邮件列表上询问。)

I'm not sure what you're trying to do. Specifically, trying to produce a file with eval-able code seems like a strange choice. In any case, creating a serialization of random values is a problem when you're dealing with structs -- since there could be different structs with the same name. Some points that might be relevant for you:

  • There is an undocumented library scheme/fasl that can read and write values in binary format (useful for large data).

  • There is also mzlib/pconvert -- a library that DrScheme uses to print values as expressions that can be evaluated (but this won't work on all kinds of data).

  • If you want to use structs that are very easy to write in a readable form, then you can use "prefab" structs.

(For more details it's probably best to ask on the mailing list.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文