Objective C 中的递归数据类型和类型

发布于 2024-11-11 15:02:28 字数 1116 浏览 2 评论 0原文

相关:
Objective C 中的惰性数据类型

从相关问题中我能够弄清楚如何使用块对象来模拟暂停计算,但我仍在尝试掌握这个概念。对于horribleComputation来说它是可行的,但是如何对无限流进行建模呢?

在 SML 中通常是如何完成的,

(* Have a datatype to wrap a computation *)
datatype 'a susp = Susp of (unit -> 'a)

(* Create a recursive datatype to handle an infinite stream  *)
datatype 'a stream = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp

现在在 Objective-C 中,有 typedef 接受预定义的值,

enum suit {hearts, spades, diamonds, clubs};

我只是不知道如何获取 Cons of 部分

现在,如果无限数据建模是不可能的,例如如何对一手牌进行建模。再次在 SML 中,

datatype suit = Clubs | Spades | Hearts | Diamonds
datatype rank = Two | Four | Five (*... etc *)

(* Then a card *)
type card = rank*suit

(* And now we can have a Hand :) *)
datatype hand = Empty | Hand of card * hand;

很可能一切都不可转移,但我只是好奇我能如何很好地使用 Objective C 进行惰性编程……例如按需处理所有自然数。我一直在寻找这个。

Related:
Lazy datatypes in Objective C

From the related question I was able to figure out how to use block objects to mimic suspended computation, but I am still trying to grasp the concept. For a horribleComputation it would work, but how would one model an infinite stream ?

How it is normally done in SML,

(* Have a datatype to wrap a computation *)
datatype 'a susp = Susp of (unit -> 'a)

(* Create a recursive datatype to handle an infinite stream  *)
datatype 'a stream = Cons of 'a * ('a stream') susp
type 'a stream = ('a stream') susp

Now in Objective-C there is typedef which takes predefined values

enum suit {hearts, spades, diamonds, clubs};

I just cannot figure out how to get the Cons of part

For now, if infinite data modelling is not possible, how would one model for example a Hand of cards. Again in SML,

datatype suit = Clubs | Spades | Hearts | Diamonds
datatype rank = Two | Four | Five (*... etc *)

(* Then a card *)
type card = rank*suit

(* And now we can have a Hand :) *)
datatype hand = Empty | Hand of card * hand;

Most likely everything is not transferable, but I am just curious how well I can use Objective C for lazy programming ... such as processing all natural numbers on demand. I have been making circles with my search for this.

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

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

发布评论

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

评论(1

北渚 2024-11-18 15:02:28

有两个独立的、正交的概念:递归数据类型和惰性计算。在 C 和类 C 语言中,您可以使用 struct 对前者进行建模,该结构包含指向自身或指向包含/指向该 struct 的其他数据类型的指针> 直接或间接。使用块对象或其他任何东西来暂停你的计算。将这两个概念结合在一起,得到一个 struct,其中包含一个指向暂停计算的指针,该计算返回(指向)该 struct 的指针。

There are two separate, orthogonal concepts: recursive datatypes and lazy computations. In C and C-like languages, you model the former with a struct that contains pointer(s) to either itself, or to other data type that contains/points to that struct directly or indirectly. Use block objects or whatever to suspend your computations. Combine the two concepts together and get a struct that contains a pointer to a suspended computation that returns (a pointer to) that struct.

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