[Haskell-cafe] Non-recursive let [Was: GHC bug? Let with guards loops]

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Jul 11 06:36:48 CEST 2013


On 10/07/2013, at 8:42 PM, Andreas Abel wrote:
>> 
>>> Hear, hear! In OCaml, I can (and often do) write
>>> 
>>>         let (x,s) = foo 1 [] in
>>>         let (y,s) = bar x s in
>>>         let (z,s) = baz x y s in ...

I really wish you wouldn't do that.

After reading Dijkstra's paper on the fact that we have
small heads many years ago -- long enough to forget the
actual title, sorry -- I realised that I too was a Bear
of Very Little Brain and Get Confused Very Easily.

I find that that when the same name gets reused like
that I get very confused indeed about which one I am
looking at right now.

If the variable is hidden (as by the DCG transformation
in Prolog, or a state monad, I don't get confused about
the variable because it isn't visible.

If each instance of the variable is labelled with a
sequence number, I don't get confused because each
variable has a different name and I can *see* which
one this is.

Yes, sequence numbering variable states is a chore for
the person writing the code, but it's a boon for the
person reading the code.

Me, I'd be perfectly happy with

setup (x,s) = state (\_ -> (x,s))

	(setup $ foo 1 []) >>= \x ->
	bar x >>= \y ->
	baz x y >>= \z ->
	...

One reason for this is that it makes refactorings like
extracting bar ... >>= ... baz ... thinkable.  A long
sequence of updates is probably crying out for such a
refactoring.




More information about the Haskell-Cafe mailing list