caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pietro Abate <Pietro.Abate@anu.edu.au>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] composing functions...
Date: Thu, 1 Dec 2005 12:38:47 +1100	[thread overview]
Message-ID: <20051201013847.GA16098@pulp.anu.edu.au> (raw)
In-Reply-To: <ad8cfe7e0511301655w2466f021yee19b32cc07ac95d@mail.gmail.com>

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


      parent reply	other threads:[~2005-12-01  1:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-01  0:55 Jonathan Roewen
2005-12-01  1:34 ` skaller
2005-12-01  2:00   ` Jonathan Roewen
2005-12-01  8:46   ` Daniel Bünzli
2005-12-01  1:38 ` Pietro Abate [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051201013847.GA16098@pulp.anu.edu.au \
    --to=pietro.abate@anu.edu.au \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).