From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 74420BBA7 for ; Thu, 1 Dec 2005 02:39:01 +0100 (CET) Received: from mail.rsise.anu.edu.au (mail.rsise.anu.edu.au [150.203.208.4]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jB11cvov016102 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 1 Dec 2005 02:39:00 +0100 Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 7A10473B4E for ; Thu, 1 Dec 2005 12:38:49 +1100 (EST) Received: from mail.rsise.anu.edu.au ([150.203.208.4]) by localhost (mail [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 14751-06 for ; Thu, 1 Dec 2005 12:38:49 +1100 (EST) Received: from pulp.rsise.anu.edu.au (pulp.rsise.anu.edu.au [150.203.208.49]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 6952D73B4B for ; Thu, 1 Dec 2005 12:38:49 +1100 (EST) Received: by pulp.rsise.anu.edu.au (Postfix, from userid 1560) id 2F2BA28CB3B; Thu, 1 Dec 2005 12:38:47 +1100 (EST) Date: Thu, 1 Dec 2005 12:38:47 +1100 From: Pietro Abate To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] composing functions... Message-ID: <20051201013847.GA16098@pulp.anu.edu.au> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: GNU/Linux X-Organization: Research School of Information Science and Engineering (Australian National University) User-Agent: Mutt/1.5.11 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at mail.rsise.anu.edu.au X-Miltered: at nez-perce with ID 438E5431.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; anu:01 caml-list:01 monads:01 sig:01 struct:01 flatten:01 struct:01 char:01 monads:01 ocaml:01 haskell:01 char:01 caml-list:01 beginner's:01 ocaml:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Let's see if I'm able to apply these 4 days of reading about monads... module ListMonadMake(T:sig type t end) = struct type mt = T.t list let return x = [x] let join mm = List.flatten mm let map f m = List.map f m let bind m f = join (map f m) let mzero = [] let mplus = List.append end module LM = ListMonadMake(struct type t = int end);; open LM ;; let apply1 (a,b,c) = [a;b;int_of_char c] ;; let apply2 a = [a] ;; let compose f1 f2 m = bind (bind (return m) f2) f1;; let f v = compose apply1 apply2 v ;; f (1,2,'a');; - : int list = [1; 2; 97] does this help ? I'm still learning, but I see a lot of potential here... How do people encode monads in ocaml ? Can you use objects to build monads similarly to Haskell ? :) pp On Thu, Dec 01, 2005 at 01:55:38PM +1300, Jonathan Roewen wrote: > Hi, > > I'm getting a bit stuck, and am wondering if there's anyway to compose > a bunch of functions together easily without having to pre-maturely > apply any of them. > > My current idea is trying to use objects, like: > > class virtual ['a] composable = object (self) > method compose a b = self#apply a @ b > method virtual apply : 'a -> int list > end;; > > class c1 = object > inherit ['a] composable > method apply (a,b,c) -> [a;b;int_of_char c] > end;; > > class c2 = object > inherit ['a] composable > method apply a -> [a] > end;; > > let o1 = new c1 and o2 = new c2;; > > I can do something like let f a1 a2 = o1#compose a1 (o2#compose a2 > []);; and get a list back... > > But what I'm wondering is if it's possible to make a generic compose > function that takes say a list of either classes or object instances, > and return a new function that I can apply the bunch of tuples to. > > Theory: let f = compose [o1;o2];; f a1 a2;; maybe it's not worth the > hassle (if it's even possible). > > Jonathan > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro ++ ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html