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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个独立的、正交的概念:递归数据类型和惰性计算。在 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 thatstruct
directly or indirectly. Use block objects or whatever to suspend your computations. Combine the two concepts together and get astruct
that contains a pointer to a suspended computation that returns (a pointer to) thatstruct
.