Do you prefer pure or Just? What does that "shepherd" you? - Haskell

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

codygman

For example given these contrived examples:

foo :: String -> Maybe Int
foo _ = ... Just 0

foo :: String -> Maybe Int
foo _ = ... pure 0

foo :: Monad m => String -> m Int
foo _ = ... pure 0

Which do your prefer?

Where does that "shepherd" you and others if anywhere?

This post explains language shepherding:

https://nibblestew.blogspot.com/2020/03/its-not-what-programming-languages-do.html?m=1

Torsten Schmits

I never use pure for Maybe, but sometimes for Either. I guess the latter has more of a monadic state character than a data character to me, compared to Maybe. depends on what the function does, how abstract it is

Stuart Terrett

I'd use Just in those exmples, but I'd use pure in a do block, even it if were explicitly Maybe.

Torsten Schmits

in that case, I think I would prefer splitting into functions and using =<<

Georgi Lyubenov // googleson78

I always prefer a specialised versions for lists, maybe, either - makes type inference in code reviews way easier

Georgi Lyubenov // googleson78

espcially for a Maybe do-block, because I don't often see Maybe do-blocks, so it takes a couple of seconds to figure out, whereas a Just immediately makes it clear

TheMatten

:plus1: - I tend to generalize once it makes sense - it's fully mechanical change and keeps used "jargon" close to actual problem

codygman

I also don't see maybe do blocks much. Is that a good or bad thing?

Sridhar Ratnakumar

pure if in do block, otherwise Just.

Mats Rauhala

I just found a maybe do block I had written earlier and thought how clever I had been

Kim-Ee Yeoh

foo should be of type String to Int. The embedding into Maybe happens at the Maybe-mandating use-site of foo’s return value. A smart type system will do that automatically for you.

Kim-Ee Yeoh
foo :: Monad m => String -> m Int
foo _ = ... pure 0

This in particular is simply wrong.

Kim-Ee Yeoh

(I am assuming that there are no branches of foo that return Nothing and that 0 is a placeholder of significant processing of the input String. Otherwise foo could be merely replaced with const 0.)

Torsten Schmits

these examples are surely just stripped down for brevity.

Kim-Ee Yeoh

Is the query merely about Just vs pure? Or does it encompass design of the type signatures? Because I am pointing out an error in the latter.