let in do - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

TheMatten

Does anyone else use "let in do" (pun intended) instead of let .. in? :

a :: Foo -- Foo is random type, it doesn't need to be Monad
a = do
  let b = bar
  let c = baz
  f b c
TheMatten

where is nice, but when it doesn't work/fit, I just go with do

Torsten Schmits

oh you mean using do without any binders

Will

I had no idea this existed...

TheMatten

Language designers probably didn't either at first - I remember reading that they actually dropped type requirement at some point

TheMatten

I mean, I wouldn't even bother implementing let .. in if I were to design new language with do

Georgi Lyubenov // googleson78

seeing as how it's syntactic sugar it doesn't make sense to have the requirement in the first place :thinking:

Georgi Lyubenov // googleson78

TheMatten said:

I mean, I wouldn't even bother implementing let .. in if I were to design new language with do

well, that trick is ugly if you're writing things that are not multiple lines

TheMatten

f a = g b where b = h a

Georgi Lyubenov // googleson78

orthogonal, the above is an expression

Georgi Lyubenov // googleson78

in general I agree that you don't see too many "same line" let-ins though

Georgi Lyubenov // googleson78

I don't mind let-in, as long as the "in part" isn't indented

TheMatten

Yeah, my experience is that I'm usually indented anyway when trying to write binding without where

Georgi Lyubenov // googleson78

well, nothing is forcing it in theory

TheMatten

I mean, technically you can even do let a = b; c :big_smile:

TheMatten

Formatters are the worst - they never understand what I'm trying to achieve :big_smile: /s

Georgi Lyubenov // googleson78

TheMatten said:

I mean, technically you can even do let a = b; c :big_smile:

this is a bit brittle though, I think? or in general many dos on the same line are

Georgi Lyubenov // googleson78

I run into issues sometimes when I'm abusing do as a ($) replacement

Torsten Schmits

really? I never had any problems with do as $ replacement

Georgi Lyubenov // googleson78

I'll try to remember to share next time it happens

TheMatten

$ let's you start at the same line and indent later at the cost of not being able to apply any other argument afterwards
do let's you unindent enough to get access to next argument, at the cost of always having to indent at the beginning if you want to use multiple lines

TheMatten

@Georgi Lyubenov // googleson78 I guess that may be the problem you're running into

Torsten Schmits

TheMatten said:

$ lets you start at the same line

same line?

TheMatten

You can't do

f do g a
  b c

though - instead you have to write

f do
  g a b c
Torsten Schmits

you can also do

f $ g a
b c
Mason Mackaman

I never use do ... let ... unless I'm naturally in a do. And I use let ... in or where depending on which is bigger, my let/where declarations or the main body of my function. I try to always put the bigger thing last.

Sridhar Ratnakumar

I avoid it, in the interest of simplicity and readability. do suggests monadic context; using it in a pure context is abusing it. I generally use where wherever possible (let is useful in cases where you want access to the closure)