What is a monad?

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
User avatar
sdetoni
Baaaaa!
Baaaaa!
Posts: 1368
Joined: Tue Jun 18, 2002 4:14 am 0
Location: New Sheepland

Post by sdetoni »

Is it me or is this whole Haskell/monads thing is a waste of time. Its like "Lets come up with so convoluted methods to program in, and call it something strange". It seems to add no value what so ever to current programming paradigms, after all we already have Lisp for the most ugly code to understand already why add to the list!

Code: Select all

hamming = 1 : map (*2) hamming # map (*3) hamming # map (*5) hamming
    where xxs@(x:xs) # yys@(y:ys)
              | x==y = x : xs#ys
              | x<y  = x : xs#yys
              | x>y  = y : xxs#ys
WTF does this mean! People should be making it easier to understand, not make it harder! I really hate languages like this; Another academic only bullshit language.

S.
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

I'm really interested in Haskell, it looks like it does complex things in a very beautiful and simple way. The syntax sucks, but like Lisp it sucks for a reason and makes sense when you get your head around it, or so I gather. I'm going to give it a proper go after spending a little more time with Scheme.*

You can tell when languages are good because mainstream languages like Ruby and Python and Java and C# and C++0x start poaching ideas. :)


*Or maybe I'll do Erlang next, or Oz or ML or F# or Scala or...
User avatar
fred_zen
Baaaa!
Baaaa!
Posts: 743
Joined: Mon Jun 26, 2006 3:15 pm 0
Location: France

Post by fred_zen »

sdetoni wrote:Is it me or is this whole Haskell/monads thing is a waste of time. Its like "Lets come up with so convoluted methods to program in, and call it something strange". It seems to add no value what so ever to current programming paradigms
Maybe it's a waste of your time. Haskell was designed by researchers as a common language for research in pure functional programming, and it has succeeded beautifully in that respect. Also, don't forget that a lot of concepts that are mainstream today were seen as convoluted in the beginning. For instance: object-oriented programming, local variables. And modern languages such as C# 3.0 or Ruby owe a lot to functional programming.
User avatar
moosedude
Baaaaaaa!
Baaaaaaa!
Posts: 6209
Joined: Fri Nov 19, 2004 1:19 pm 0
Location: Craggy Island
Contact:

Post by moosedude »

fred_zen wrote:
sdetoni wrote:Is it me or is this whole Haskell/monads thing is a waste of time. Its like "Lets come up with so convoluted methods to program in, and call it something strange". It seems to add no value what so ever to current programming paradigms
Maybe it's a waste of your time. Haskell was designed by researchers as a common language for research in pure functional programming, and it has succeeded beautifully in that respect. Also, don't forget that a lot of concepts that are mainstream today were seen as convoluted in the beginning. For instance: object-oriented programming, local variables. And modern languages such as C# 3.0 or Ruby owe a lot to functional programming.
I remember being forced to learn a pure programming language at university called Miranda. It required a certain mindset you had to switch into before you could start getting it to dance. I suspect if you were that type of person, or had come from the correct background (Pure Maths?) it would of been easier for you. The most difficult part was coping with the lack of temp. storage variables and thinking in terms of recursion (which not all problems lend themselves easily to a solution in this vein, if many)

But I'm with sdetoni on this one, what really are the practical benefits of writing in such a language, who really uses this? In the world I come from I write windows applications & websites, I implement everything in nice neat classes.. obviously from my point of view writing something like this would be near impossible in a pure language, but what else can you do with such a creation? a real world example.

01000101 01000100 01000111 01000101

http://www.redbubble.com/people/fotofill/ buy my photos!
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

There are two (putative) practical benefits for functional programming:

An awful lot of bugs come from having side effects. In a pure functional language you don't have these, so you immediately strip out the source of potential bugs.

Functional programming makes you think about the problem rather than the implementation. It forces you to get things straight in your head, then maps (haha!) more or less directly to how your brain works when solving a problem.

I'm a complete convert to functional programming. Even in languages where it's difficult to use a functional style, programming in a functional way just makes everything so much clearer and more elegant. It's not much help where the implementation matters though, but that's pretty rare these days (I can't remember where I put it, but I posted a link a few weeks ago where Tim Sweeney was considering writing a future UnrealEngine in a Haskell-like language).
User avatar
moosedude
Baaaaaaa!
Baaaaaaa!
Posts: 6209
Joined: Fri Nov 19, 2004 1:19 pm 0
Location: Craggy Island
Contact:

Post by moosedude »

Hmm, you've trigger a memory. I seem to remember that linklist traversal can be implemented extremely elegently. I think you had to do a little bit of magic to get Miranda to do a linklist, but it could be done.

I also remember using it on really rubbish unix terminal servers, and after a certain amount of recursion it would bomb out because you run out of stack memory :lol:

01000101 01000100 01000111 01000101

http://www.redbubble.com/people/fotofill/ buy my photos!
User avatar
lis0r
Baaaa!
Baaaa!
Posts: 894
Joined: Tue Jan 10, 2006 9:26 am 0
Location: The Frozen Wastelands Of The North

Post by lis0r »

sdetoni wrote:People should be making it easier to understand, not make it harder!
Why?

Nobody said the language was for newbies. Why should it impair it's functionality just for their benefit.

Haskell is a hard-as-nails language for expressing hard-as-nails compsci theories. Elegance, clarity and expressiveness are the trumps here. (And if you don't think it has them, just try expressing the same ideas in COBOL at the fundamental level.)
User avatar
lis0r
Baaaa!
Baaaa!
Posts: 894
Joined: Tue Jan 10, 2006 9:26 am 0
Location: The Frozen Wastelands Of The North

Post by lis0r »

moosedude wrote:But I'm with sdetoni on this one, what really are the practical benefits of writing in such a language, who really uses this?
Computer scientists.
In the world I come from I write windows applications & websites, I implement everything in nice neat classes..
Wrong viewpoint. The angle of a computer scientist should be "What do I need to know to rebuild the state of the art if the Americans turned the Earth into a smouldering ball of glass, assuming I survived?"

Personally, I'm all in favour of the next generation saying "Oh, why do I need to know how to do that, my virtual machine standard library does it for me!" Principally, because it guarantees me a long career in building virtual machine standard libraries, etc...
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

This just popped up on reddit :)

Image
User avatar
sdetoni
Baaaaa!
Baaaaa!
Posts: 1368
Joined: Tue Jun 18, 2002 4:14 am 0
Location: New Sheepland

Post by sdetoni »

lis0r wrote:
sdetoni wrote:People should be making it easier to understand, not make it harder!
Why?

Nobody said the language was for newbies. Why should it impair it's functionality just for their benefit.

Haskell is a hard-as-nails language for expressing hard-as-nails compsci theories. Elegance, clarity and expressiveness are the trumps here. (And if you don't think it has them, just try expressing the same ideas in COBOL at the fundamental level.)
I still fail to see how functional programming is any better than normal C/Java etc type languages. You may condense your program/problem smaller, but in doing so add extra complexity to the syntax of the language. While the other hand languages like C have a small and elegant syntax (can't say the same for C++ though) but its abilities are great.

If you want to write your program that looks more like some made up math code as oppose to simple logic, data flow, then more power to you. I prefer to be able to understand a function's program/data flow as oppose to decrypting code.

Even a simple program in Haskell is confusing .... why does it need to be, what are you actually saving programming in Haskell instead of perl/C/java ... keystrokes?

Code: Select all

--
-- written in Haskell 1.4 (Hugs 1.4 for Windows)
-- by Michael Neumann
--

module Squares (main) where

square :: [Int] -> String
square []    = "\n"
square (h:t) = show (h*h) ++ " " ++ square t

main = putStr (square [1..10])
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

sdetoni wrote:I still fail to see how functional programming is any better than normal C/Java etc type languages. You may condense your program/problem smaller, but in doing so add extra complexity to the syntax of the language. While the other hand languages like C have a small and elegant syntax (can't say the same for C++ though) but its abilities are great.
Functional programming has nothing to do with the syntax. Haskell's syntax is enigmatic and offputting, as is Lisp's to a lesser degree. The problem is that functional programing languages have historically been developed by mathematicians so they tend to be a bit twisted.

Javascript, Lua and Ruby all allow functional programming to a reasonable degree. Anything with a flexible syntax and lambda. But having said that, I work with a custom imperative language with a very readable BASIC-like syntax and structure that translates almost directly into bytecode - about as far from a functional programming language as you can get - and like I've said before, writing in a quasi-functional style helps me to write clear, bug-free code.
If you want to write your program that looks more like some made up math code as oppose to simple logic, data flow, then more power to you. I prefer to be able to understand a function's program/data flow as oppose to decrypting code.
The difference between the functional style and the imperative style is that with the functional style you make the implementation as abstract as possible, stick with the logic, then work your way down only if necessary. Once the logic is sound then the rest of the program will follow naturally and quickly. If you work in a purely functional style then you can actually prove mathematically that a program will do what you intend it to.

If you're writing a sort algorithm, you don't want to be mucking about with memory allocation or how you're going to insert numbers into a sorted array - you just want to work on the logic. If speed becomes a problem you can optimise it later and the structure should mean that refactoring is minimal.
Even a simple program in Haskell is confusing .... why does it need to be, what are you actually saving programming in Haskell instead of perl/C/java ... keystrokes?

Code: Select all

--
-- written in Haskell 1.4 (Hugs 1.4 for Windows)
-- by Michael Neumann
--

module Squares (main) where

square :: [Int] -> String
square []    = "\n"
square (h:t) = show (h*h) ++ " " ++ square t

main = putStr (square [1..10])
This code creates a Squares module containing two functions. square takes a list of integers ([Int]) and outputs a string (String). If square has no arguments (an empty list - []) it returns a newline, otherwise it calls the first list element h and dumps the rest in a list called t. It then formats the value of h*h into a string, adds a space and recursively adds the rest of the parsed list. When there's no numbers left the t list will be empty and so that's the end of the string. The main function dumps it to the output.

Where Haskell differs from other languages is that the list of numbers [1..10] is a purely abstract term - it's not actually an array of 10 integers, it's a description of an array where the elements only become real when needed. You can easily work over a list of millions of elements and not run out of stack space.
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

I found this interesting :arrow: http://www.haroldtherebel.com/2007/12/0 ... ngers-cat/

The box containing Schroedinger's Cat is a monad. :)
User avatar
nebogeo
Baa!
Baa!
Posts: 75
Joined: Wed Mar 31, 2004 10:11 pm 0
Contact:

Post by nebogeo »

The reason Haskell is such a strict language in terms of side effects is exactly the same reason C/C++/Java etc are strict in terms of static typing - it prevents bugs, and makes it easier to write compilers to make things more efficient. Pure functional programming just ramps up the strictness - monads are needed to have little workarounds for these rules (I think a bit like unsafe code which uses pointers in C#).

Like static typing, pure functional programming forces you to think clearly about the problem in hand, and helps prevent the construction of complex programs sliding into a mess :)

It also might come in very useful in a world where concurrency is much more important than it is now.

Personally I don't like this strictness, I'm more a dynamic typing guy - I prefer flexibility - but they are all good approaches.
User avatar
tufty
Sheepie Whore
Sheepie Whore
Posts: 12524
Joined: Fri Jan 07, 2005 4:52 pm 0
Location: French Alps

Post by tufty »

teamonkey wrote:If you work in a purely functional style then you can actually prove mathematically that a program will do what you intend it to.
Doesn't Gödel come round to bite you on that one?

Simon
"massive retard™", "full of shit™", "tiny bitch™", "retarted™", "a bit of a one trick pony™", "petty™", "simple minded idealistic twat™", "miserable humourless fucker™"
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Post by teamonkey »

tufty wrote:Doesn't Gödel come round to bite you on that one?
It shouldn't do - there are certain base functions that you have to take to be axiomatic. My formal logic's not so good though.
Post Reply