Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* lazy application to Lazy.t
@ 2004-12-07 21:41 Hal Daume III
  2004-12-08  0:29 ` [Caml-list] " Karl Zilles
  0 siblings, 1 reply; 2+ messages in thread
From: Hal Daume III @ 2004-12-07 21:41 UTC (permalink / raw)
  To: Caml Mailing List

suppose I have

  val x : int ref Lazy.t

and I want to 'incr' it, but want to keep it lazy.  i.e., if i start with:

  let x = Lazy.lazy_from_fun (fun () -> ref 0)

then i run my lazy_incr on it, I want:

  let y = Lazy.force x

to return 1

but i only want 'incr' to be run when I Lazy.force x.  is there a way to 
accomplish this?

  

-- 
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] lazy application to Lazy.t
  2004-12-07 21:41 lazy application to Lazy.t Hal Daume III
@ 2004-12-08  0:29 ` Karl Zilles
  0 siblings, 0 replies; 2+ messages in thread
From: Karl Zilles @ 2004-12-08  0:29 UTC (permalink / raw)
  To: Hal Daume III; +Cc: Caml Mailing List

Hal Daume III wrote:
> suppose I have
> 
>   val x : int ref Lazy.t
> 
> and I want to 'incr' it, but want to keep it lazy.  i.e., if i start with:
> 
>   let x = Lazy.lazy_from_fun (fun () -> ref 0)
> 
> then i run my lazy_incr on it, I want:
> 
>   let y = Lazy.force x
> 
> to return 1
> 
> but i only want 'incr' to be run when I Lazy.force x.  is there a way to 
> accomplish this?

Not the way you have it.  Because x is itself not a reference, you can't 
change its value by calling a function on it.

If instead you had val x : int Lazy.t ref, then it would be possible:

open Lazy;;

let x = ref (lazy (0));;
val x : int lazy_t ref = {contents = <lazy>}

let lazy_incr r = let current = !r in r:=lazy ((force current) + 1);;
val lazy_incr : int Lazy.t ref -> unit = <fun>

lazy_incr x;;
- : unit = ()

force !x;;
- : int = 1



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-12-08  0:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-07 21:41 lazy application to Lazy.t Hal Daume III
2004-12-08  0:29 ` [Caml-list] " Karl Zilles

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox