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