pyret-lang
pyret-lang copied to clipboard
Add a `thunk` construct?
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.
Is thunk: ... end any better than lam(): ... end, which already exists?