[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-12-01 (01:39) |
From: | Pietro Abate <Pietro.Abate@a...> |
Subject: | Re: [Caml-list] composing functions... |
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