Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:43:41+00:00 2026-05-22T01:43:41+00:00

In SML, the following is possible for modelling lazy programming, // Have a datatype

  • 0

In SML, the following is possible for modelling lazy programming,

// Have a datatype to wrap a computation
datatype 'a susp = Susp of (unit -> 'a)
// A function to hold the computation
fun delay(f ) = Susp(f)

I know that closures can be written using Blocks,

int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
    return num * multiplier;
};

So I think I can use it as a function argument. The next step would be how to use functions with no real arguments ( unit value e.g. in SML fn () =>) and creating lazy datatypes as the one above.

Is this possible or should I be pursuing a different more obvious way ?

The end goal would be to emulate the suspended computation behaviour from SML,

let val x = Susp(fn () => horribleComp(345))
in 
   force(x) + force(x)
end

where force(x) is

fun force (Susp(f)) = f ()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:43:42+00:00Added an answer on May 22, 2026 at 1:43 am

    Cool question!

    You could implement a lazy container in Objective-C as follows (but you probably shouldn’t, see below):

    typedef id (^SuspBlock)(void);
    
    @interface Susp : NSObjecti
    - (id)initWithBlock:(SuspBlock)block;
    + (id)withBlock:(SuspBlock)block;
    - (id)force;
    @end
    
    // -----
    
    @interface Susp ()
    @property (nonatomic, copy) SuspBlock _block;
    @end
    
    @implementation Susp
    @synthesize _block;
    
    - (id)initWithBlock:(SuspBlock)block {
      self = [super init];
      if (self != nil) {
        self._block = block;
      }
    
      return self
    }
    
    + (id)withBlock:(SuspBlock)block {
      return [[[self alloc] initWithBlock:bloc] autorelease];
    }
    
    - (id)force {
      return self._block();
    }
    
    - (void)dealloc {
     self._block = nil;
     [super dealloc];
    }
    
    @end
    

    That’s a lot of boilerplate, but whatever. Then, you could use it like this:

    id x = [Susp withBlock:^{ return someComputation(); }];
    id result = [[x force] plus:[x force]];
    // assuming the result of your computation has -plus:
    

    But that’s all rather silly, since for what you’re doing, you really don’t need another data type. Just use blocks as your datatype:

    typedef id (^SuspVal)(void);
    SuspVal x = ^{ return complicatedThing; };
    id result = [x() plus:x()];
    

    That’s a much more compact, idiomatic way of going about it, and it’s what I suggest. Unless you need to add further semantics to your lazy objects that go beyond the basic utilities of blocks, you shouldn’t wrap them needlessly.

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to make a tail-recursive version of this very simple SML function:
I'm getting what seems to be a precedence issue in calling an SML function,
I have an assignment where I need to implement church numerals in SML using
Hey, im very new to SML and programming alltogether, i want to write a
i want to write a function that checks for equality of lists in SML
I am attempting to compare files in a directory using SML. Using the TextIO
For the following code: <% foreach (Entities.Core.Location loc in locations){ %> <div class=place_meta> <img
I am using the following form builder ( http://dontlink.me/formbuilder/ ) and making lots of
I see that the SML/NJ includes a queue structure. I can't figure out how
I'm just starting out with SML, and I'm trying to modify some code so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.