pyret-lang icon indicating copy to clipboard operation
pyret-lang copied to clipboard

Add a `thunk` construct?

Open shriram opened this issue 5 years ago • 1 comments

Is it possible to add a thunk construct? As I'm doing more streams in cs019, it'd be nice to not have to write the alphabet soup that is {(): …} over and over.

fun lz-cons<A>(f :: A, r :: Stream<A>) -> Stream<A>:
  lz-link(f, thunk: r end)
end

fun lz-map<A>(func :: (Number -> A), s :: Stream<Number>) -> Stream<A>:
  lz-link(func(lz-first(s)), thunk: lz-map(func, lz-rest(s)) end)
end

[Racket has it: https://docs.racket-lang.org/reference/procedures.html?q=thunk#%28form._%28%28lib._racket%2Ffunction..rkt%29._thunk%29%29]

One could imagine also adding delay, à la Scheme, which explicitly caches the value, resulting in big-O differences. But in the presence of state, having an un-cached primitive is also nice.

shriram avatar Oct 16 '20 17:10 shriram

Is thunk: ... end any better than lam(): ... end, which already exists?

blerner avatar Oct 16 '20 22:10 blerner