brownplt / pyret-lang

The Pyret language.
Other
1.07k stars 111 forks source link

Add a `thunk` construct? #1536

Open shriram opened 4 years ago

shriram commented 4 years ago

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.

blerner commented 4 years ago

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