Is there a nicer way to write a self referential let expression

Viewed 77

i.e. can we write the following more concisely?

bar in_env a = let out_env = foo in_env (R a out_env) in out_env

I want to write it something like

bar in_env a = foo in_env (R a ??)

But maybe it isn't possible?

1 Answers

First, rewrite it like this, so that you'll be able to easily eta-reduce away out_env in the next step:

bar in_env a = let out_env = foo in_env . R a $ out_env in out_env

Then, use the fact that recursive let is equivalent to fix:

import Data.Function (fix)
bar in_env a = fix $ foo in_env . R a
Related