News for hackers. Stuff that matters.
Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @02:09PM (#32322283)
Can you explain monads to a 5-year old? Can you explain objects?
Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @02:18PM (#32322388)
I don't know if you're being serious or not, but you can learn a ton of FP without having to understand monads. Also asking to explain "objects" is too ambiguous. an "object" in json is very simple. OOP on the other hand involves a mountain of different concepts, design patterns, best practices, etc. I would guess GP is referring to that stuff.
Re:Why study functional programming? (2012) by LudwigNagasena on Tuesday August 02, 2022 @02:33PM (#32322574)
You can learn a ton of OOP without having to dive into a mountain of different concepts too.
Re:Why study functional programming? (2012) by yawaramin on Wednesday August 03, 2022 @07:13PM (#32337554)
You can learn a ton of FP without having to dive into a mountain of different concepts too.
Re:Why study functional programming? (2012) by pmontra on Tuesday August 02, 2022 @05:40PM (#32324622)
You can write OOP code without knowing a single pattern and whilst laughing at Factory classes you read in someone else's code. You might have to reinvent the wheel sometimes and face some difficulties talking with people that know every single pattern.

And there are best practices for every single language and paradigm.

Re:Why study functional programming? (2012) by unsafecast on Tuesday August 02, 2022 @02:59PM (#32322860)
Yes, I can definitely explain monads to a 5-year old.

See, a monad is like a box. You can put something in the box and close it very easily. But once it's closed, you can't open it anymore. The action of closing the box is called the wrap operation.

What you _can_ do though, is tell someone else to do something to the thing inside the box. For example, if you put a toy in and close the box, you can ask a friend to go ahead and add a new one in. Or remove the toy. These are monadic functions.

That's it. That's literally it. It's just a pattern of hiding the data and letting monadic functions be the only ones that deal with it.

In the case of the Maybe monad for example, you'd have the unwrap function that tries to get whatever is inside out, but it might not be able to (if the box is empty).

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @03:23PM (#32323133)
Not going to lie, I don't understand that explanation. It honestly sounds more like you are describing a form of encapsulation, as the contents of the box are hidden and someone else has the ability to open said box to get access to it.

I also find the appeal to a physical object to explain a functional concept is rather amusing.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @03:30PM (#32323210)
Not sure if you are looking for an explanation of Monads, but Monad tutorials are a long running joke in the haskell world. They are almost exclusively written by people for whom the concept just clicked and they either fail provide context for someone without that experience or they fail to actually describe the concept correctly.

I'm not gonna give you an explanation but I will say that IMO the best way to actually learn about them is to just look at the typeclass and a bunch of instances of it and how they are used. If you ignore all the theory (and the "theory") and just look at the code you will find that they aren't that hard to sort out from a usage perspective.

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @03:33PM (#32323244)
Apologies, I seem to have deleted a sentence where I think I can make my understanding fit that metaphor. I don't think they are completely unapproachable, but I am not at all clear that a physical metaphor works. And, I really wanted to shine a light on the amusing appeal to physical objects in a thread that is about the short comings of object oriented programming. :D
Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:45PM (#32324672)
No need for apologies! I 100% agree that the physical metaphor doesn't work and said so in another nearby comment.

If you go back to Phil Wadler's original paper on implementing monads in haskell, he doesn't talk about category theory or boxes or anything. He lays out a handful of common things you might do in programming but which seem totally unrelated. For each one he implements a solution and then reveals that all these solutions fit the same interface.

Re:Why study functional programming? (2012) by trebbble on Wednesday August 03, 2022 @02:43PM (#32335359)
At some point, it dawned on me that my problem wasn't that I didn't understand monads, it's that the idea's so simple I couldn't convince myself I understood it, and that what I actually didn't understand was why anyone gave a shit about them.

Like if a bunch of smart people keep going on and on about something called a "floobiz", and every time they try to explain it to you, it just looks like a fucking cup. The problem isn't that you don't understand what a cup (floobiz) is, it's that you don't get why they keep going on about them like they're something special.

Re:Why study functional programming? (2012) by ModernMech on Tuesday August 02, 2022 @04:10PM (#32323684)
> I also find the appeal to a physical object to explain a functional concept is rather amusing.

The challenge was to describe it to a 5 year old. 5 year olds are better with concrete thinking than abstract reasoning. This is what made using the turtle in Logo a genius move, as it allowed younger children to write programs by conceptualizing a physical turtle that can move, rather than thinking in terms of an abstract function that mutates data.

Re:Why study functional programming? (2012) by taeric on Tuesday August 02, 2022 @04:19PM (#32323772)
Fair, and I don't necessarily disagree. I think this leads rather close to object oriented software being easier to reason about concretely than functional programming, though. And since most people are trying to get a concrete job done, that feels like a significant claim.

For turtle geometry, it is also important to acknowledge the metaphor and shortcomings of a more traditionally analytic framing of drawings. Specifically, X and Y coordinates for a drawing are surprisingly hard to work with. For example, I challenge you to describe the fractal snow flake in an easier way than using directions as a metaphor. Same for the dragon curve. This can be seen akin to picking a different coordinate system, I presume?

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @03:25PM (#32323145)
Unfortunately the container metaphor only works for a subset of monads. It fails with `IO`, `Reader`, `Writer`, `State`, `Parser`, etc.
Re:Why study functional programming? (2012) by AnimalMuppet on Tuesday August 02, 2022 @03:36PM (#32323272)
Well, for IO, it seems to me that maybe it still works. You're putting the entire external world in the box, but you're still (kind of?) doing the same thing.
Re:Why study functional programming? (2012) by unsafecast on Wednesday August 03, 2022 @03:48AM (#32328793)
You might be right. I'll try a different way:

You want to assemble a 16-piece puzzle, but there's too many of your friends around for you all to work on it at once, it would be a disaster. So you invent a game. The rules are:

- Everyone only gets to put a single piece. There's 16 of your friends, so it works great.

- You all sit in a row on the floor, and the puzzle gets passed from the last to the next.

- Nobody can talk. All you can do is put a piece in and pass the puzzle.

You then realize that there's actually 17 of you (you have 16 friends and forgot to count yourself) so every turn one of you sits at the end, receives the finished puzzle and checks it.

It works great the first few times (you have _a lot_ of puzzles), but then something happens. One of your friends lost a piece. They can't talk, so they just panic and the turn ends.

What you need to do is change the game a bit so this doesn't happen again (it's better to get no puzzle than a panicked friend). So you add two rules:

- If you can't add your piece for some reason, you pass a piece of paper saying why instead of the puzzle.

- If you receive a piece of paper, pass it to the next person.

With everyone equipped with the paper and the piece, the next turn starts. Your friend lost their piece again, but there's no problem: you've got a lot of puzzles, and more importantly, your friend knows what to do. They write ‘I lost my piece’, and give the paper to the next person.

You, sitting at the end, receive the piece of paper and know exactly what happened.

Congratulations, that's the Either monad with a puzzle as the Right, and a piece of inscribed paper as the Left, and the ‘bind’ operation is the rule set that explains how you communicate with the next person.

You can use the same kind of explanation for State, except you come up with a game that uses the same object as the state and every ‘friend’ makes something different that the next one needs.

Note: Saying you lost your piece out loud would be like throwing an exception. I guess you can re-use this as an explanation for that.

Re:Why study functional programming? (2012) by solomonb on Wednesday August 03, 2022 @02:12PM (#32335081)
I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

I honestly think the best way to explain the Monad typeclass is by just showing the code. It really isn't that complex:

    instance Monad (Either e) where
      return = Right
      Left err >>= _ = Left err
      Right a >>= f = f a
And then show how you can use `>>=` to chain operations on `Either` values. Then you show a second instance, such as:

    instance Monad (State s) where
      return a = State $ \s -> (s, a)
      ma >>= f = State $ \s -> let (s', a) = runState ma s in runState (f a) s'
Then explain how you can use `>>=` here to do something seemingly completely different.

Damnit, now I've accidentally written a monad tutorial :(

Re:Why study functional programming? (2012) by unsafecast on Thursday August 04, 2022 @12:15PM (#32344829)
> I'm sorry, I know you put a lot of effort into this but I find it rather convoluted.

Don't worry about it! I'm very happy to receive criticism.

I tried to do it in this way because the task was to explain it to a 5-year old. I haven't (yet?) met a person that young who could understand an abstract language like Haskell. It's just not happening.

My explanation is probably indeed way too convoluted, but I still believe monads can be intuitively explained to a 5-year old somehow.

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @03:43PM (#32323331)
how about we call the box wrapper and the operations on it computations?

metaphors are good for beginners, to grasp the concept, they don't need to be perfectly valid.

for example the popular OOP metaphor a car is a vehicle becomes useless pretty much immediately.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:47PM (#32324693)
IMO the metaphor is already getting too frayed at this point.

I'm not opposed to metaphors in general, but metaphors are lot like abstractions. If you don't get the right one then the details leak everywhere and you may as well have not used it to begin with.

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @02:47PM (#32322732)
monads are a very simple pattern

it takes 5 minutes to understand it, the jargon is complex, the implementation is not.

case in point

https://youtu.be/C2w45qRc3aU

objects are a very leaky abstraction that usually brings people to build taxonomies, mostly unrelated to the parent.

reuse of code, which is the selling point of OOP,also comes often short with a miriad of very specialized subclasses that have nothing in common anymore.

OOP is not horrible, but it requires a good amount of discipline to get it right, while FP has less concepts and you can (usually) silo the "and now for the tricky bits" (cit. Robert Virding) in a small core.

the rest is simple pure functions that have also the benefit of being stupid trivial to test.

Also, you don't have to be purely functional nowadays, and that helps a lot.

If only I learned FP sooner, my life as a programmer would have been so much better.

Re:Why study functional programming? (2012) by freedomben on Tuesday August 02, 2022 @03:29PM (#32323202)
You're point about testability is a good one that I feel is often missed. Trying to test functions in OOP code is often impossible because they presuppose some state in the "object" and if it's not initialized properly, it doesn't work. Then they also often make side effects. So you end up building a huge test harness to build up some state, run one test, and tear it all back down again only to rebuild it from scratch for the next test. It's doable of course, but it's easy for a simple CRUD app to have a 15 minute test suite and for tests to become flaky because of some unexpected state change done in some unrelated test on the other side of the code base. That's a special form of hell
Re:Why study functional programming? (2012) by goostavos on Tuesday August 02, 2022 @05:03PM (#32324282)
Monads are indeed way simpler. Just a few beautiful laws. The problem is that they take a loooooooooooong time to really internalize. Saying it takes "5 minutes" is empty in the same way that "learn C++ in 24 hours" claims are (or you're hella smart!).

There's a quote that floats around that's like "as soon as you understand monads, you lose the ability to explain monads." It takes a ton of leg work to make them click. They're simpler, but definitely not easy (in the Rich Hickey sense).

Re:Why study functional programming? (2012) by bitL on Tuesday August 02, 2022 @05:23PM (#32324453)
Ok, so you understood flatmap and therefore monads in 2 minutes. Now you need something that needs nested monads. That's where major complexity suddenly appears - your code is no longer simple and is full of artificial constructs like transformers just to do simple things. Moreover, anyone who reads your code must know all language constructs you are using or they are lost and can't fix any issue in your code.
Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @05:50PM (#32324719)
> Moreover, anyone who reads your code must know all language constructs you are using or they are lost and can't fix any issue in your code.

Does this not apply to every language ever?

Re:Why study functional programming? (2012) by peoplefromibiza on Tuesday August 02, 2022 @05:08PM (#32324326)
5 minutes is the time it takes to understand how the pattern works.

Be good at it?

More like a thousand hours.

there is nothing that is really simple, but before being simple, everything looks difficult.

p.s. I love Rich Hickey and especially his keynotes at Clojure conferences.

I've watched simple made easy at least 10 times.

Re:Why study functional programming? (2012) by solomonb on Tuesday August 02, 2022 @02:17PM (#32322378)
I don't think that is a fair comparison. Monad is a typeclass so it would be like explaining some specific Java Interface (and all the context for Interfaces) to a 5 year old.

The Haskell (since you brought up Monads) equivalent to explaining Objects would be like explaining Records or Sum types, which would be very easy to explain and don't require talking about inheritance like Objects would.

Re:Why study functional programming? (2012) by justinhj on Tuesday August 02, 2022 @09:15PM (#32326505)
Some functions return plain values. Other functions return fancy values with the plain values inside. There are different types of fanciness. One simple kind is called option. An option may have a plain value inside it or it may be nothing. Imagine looking up a phone number in the phone book. You get the name wrapped in an option or nothing because the person is not listed. Another fancy kind of thing is a Future. It runs some operation like looking up something in a database. When you call the function it just returns this fancy thing and at some point the database sends a response and that response is a plain value inside it. If you want the plain value you have to wait for the future. Well quite often you have functions that return fancy things and you would like to chain them together; lookup some user and then lookup their account. Instead of waiting for the first one and getting the plain value out then passing that to the second one and waiting for that, you could write some code that chained the two functions together for you. That composing of functions that return fancy things is called join or flatmap. If you also have a function that can put any value into a fancy thing (this is called unit or pure) you now have what is called a monad.
Re:Why study functional programming? (2012) by mejutoco on Tuesday August 02, 2022 @03:48PM (#32323405)
I’ll try.

A monad is a description of actions to do and their order, in place of the actual actions. This description can be passed around and eventually acted upon.

Objects (not Alan Kay’s original ones but say Java) are abstractions that model state and behaviour together. Objects can share common behaviour through interfaces

Re:Why study functional programming? (2012) by mdcds on Tuesday August 02, 2022 @03:49PM (#32323426)
I heard following explanation of monads somewhere online: "monads allow us to write imperative code in the functional setting. e.i. chain computations".

But that explanation wouldn't make sense to a 5-year-old