From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.7 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id C14EFBBAF for ; Tue, 22 Jul 2008 14:58:10 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.31,231,1215381600"; d="asc'?scan'208";a="15379195" Received: from peray.inria.fr (HELO ausone.inria.fr) ([128.93.8.98]) by mail1-relais-roc.national.inria.fr with SMTP; 22 Jul 2008 14:58:10 +0200 Received: by ausone.inria.fr (sSMTP sendmail emulation); Tue, _d Jul 2008 14:57:36 +0200 From: "Nicolas Pouillard" Cc: Caml_mailing list Subject: Re: [Caml-list] Disappointment To: Paolo Donadeo References: <4b5157c30807211428r19ef9865n6a65e81ac2f5fe31@mail.gmail.com> In-Reply-To: <4b5157c30807211428r19ef9865n6a65e81ac2f5fe31@mail.gmail.com> Date: Tue, 22 Jul 2008 14:57:31 +0200 Message-Id: <1216730478-sup-1986@port-ext16.ensta.fr> User-Agent: Sup/git Content-Type: multipart/signed; protocol="application/pgp-signature"; boundary="=-1216731456-199601-11534-9332-3-="; micalg="pgp-sha1" MIME-Version: 1.0 X-Spam: no; 0.00; monads:01 monads:01 val:01 val:01 abstractly:01 aroused:98 wrote:01 arnaud:01 abstract:01 caml-list:01 int:01 int:01 match:02 match:02 functional:02 X-Attachments: cset="UTF-8" type="application/pgp-signature" name="signature.asc" name="signature.asc" --=-1216731456-199601-11534-9332-3-= Content-Type: text/plain; charset=UTF-8 Excerpts from Paolo Donadeo's message of Mon Jul 21 23:28:36 +0200 2008: > I'm disappointed with myself and my incredibly low IQ. Late this > evening I decided -- and this is the third time -- to be enlightened > by the concept of monad. > > I like functional programming, but monads [1] must be too little to be > grabbed by my mind. This time the interest in monads was aroused by > the interesting article of David Teller, Arnaud Spiwack and Till > Varoquaux [2] about the error monad, but for using the library they > wrote I need at least some knowledge about monads and the do-notation. > > I tried with some tutorials found around, but I still cannot catch the > point: what the hell is a monad? Two key points that helped me: * Monads help to separate some plumbing from your code. * Monads provide a way to abstract code over some "let" construct. I will note that specific "let" construct "let!", it's somewhat like the do-notation but more atomic. Monads also come with "return", but that's not the essence of them. Think about that example: val div : int -> int -> int option val square : int -> option let f x y = match div x y with | None -> None (* here 'y' was equal to 0 *) | Some z -> match square z with | None -> None | Some x2 -> Some x2 In the previous example the plumbing is error handling (where errors are represented by None), and the "let!" construct is: let! x = e1 in e2 ===> match e1 with None -> None | Some x -> e2 And "return" is "Some". Another similar example: type ('a,'b) either = Left of 'a | Right of 'b val div : int -> int -> (string, int) either val square : int -> (string, int) either let f x y = match div x y with | Left error_msg -> Left error_msg | Right z -> match square z with | Left error_msg -> Left error_msg | Right x2 -> Right x2 Here the plumbing is still there for "error handling", but is somewhat different. The "let!" construct is: let! x = e1 in e2 ===> match e1 with Left m -> Left m | Right x -> e2 And "return" is "Right". Finally one could have used the "let!" construct abstractly (and also "return"). let f x y = let! z = div x y in let! x2 = square z in return x2 One can obtain the two previous versions by choosing which "let!"/"return" we want, namely choosing the monad. Hope that helps, -- Nicolas Pouillard aka Ertai --=-1216731456-199601-11534-9332-3-= Content-Disposition: attachment; filename="signature.asc" Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkiF2TsACgkQj+FCNw9dwLlliwCgkCPc6EMKZsL6XXi0D1T562pn lecAn24e1cMejTLh19+gWpaaZMn/4s2B =xGI/ -----END PGP SIGNATURE----- --=-1216731456-199601-11534-9332-3-=--