Take the 2-minute tour ×

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer


