caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Rich Neswold <rich.neswold@gmail.com>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Smart ways to implement worker threads
Date: Thu, 15 Jul 2010 14:56:21 -0500	[thread overview]
Message-ID: <AANLkTik3C58pheUssb8LDtvj3gkcA3qoKT0YsaQ65B6N@mail.gmail.com> (raw)
In-Reply-To: <87zkxsfvsg.fsf@frosties.localdomain>


[-- Attachment #1.1: Type: text/plain, Size: 850 bytes --]

On Thu, Jul 15, 2010 at 1:24 PM, Goswin von Brederlow <goswin-v-b@web.de>wrote:

> It is too bad I don't want to learn CML but use Ocaml. The CML examples
> from the book don't translate into ocaml since the interface is just a
> little bit different and those differences are what throws me off.
>


> So could you give a short example? E.g. the merge sort from the book.
>

You're right: OCaml's syntax differs enough from the text that it's annoying
to cut-n-paste the examples. Fortunately most example are a few lines of
code, so I didn't realize how difficult it could be on a larger example
(like the merge sort example.)

Attached is my translation of the mergeSort from the book. You get to play
with it and see if it works  :)

-- 
Rich

Google Reader: https://www.google.com/reader/shared/rich.neswold
Jabber ID: rich@neswold.homeunix.net

[-- Attachment #1.2: Type: text/html, Size: 1444 bytes --]

[-- Attachment #2: mergesort.ml --]
[-- Type: text/x-ocaml, Size: 1487 bytes --]

open Event

let mySend c d = sync (send c d)
and myRecv c = sync (receive c)

let spawn f =
  ignore (Thread.create f ())

let split (inCh, outCh1, outCh2) =
  let rec loop = function
    | (None, _, _) -> (mySend outCh1 None; mySend outCh2 None)
    | (x, out1, out2) -> (mySend out1 x; loop (myRecv inCh, out2, out1))
  in
    loop (myRecv inCh, outCh1, outCh2)

let merge (inCh1, inCh2, (outCh : int option channel)) =
  let rec copy (fromCh, toCh) =
    let rec loop v =
      begin
	mySend toCh v;
	match v with
	  | Some _ -> loop (myRecv fromCh)
	  | None -> ()
      end
    in
      loop (myRecv fromCh)
  and mergep (from1, from2) =
    match (from1, from2) with
      | (None, None) -> mySend outCh None
      | (_, None) -> (mySend outCh from1; copy (inCh1, outCh))
      | (None, _) -> (mySend outCh from2; copy (inCh2, outCh))
      | (Some a, Some b) ->
	  if a < b then
	    (mySend outCh from1; mergep (myRecv inCh1, from2))
	  else
	    (mySend outCh from2; mergep (from1, myRecv inCh2))
  in
    mergep (myRecv inCh1, myRecv inCh2)

let rec mergeSort () =
  let ch = new_channel() in
  let sort () =
    (match myRecv ch with
       | None -> ()
       | v1 ->
	   begin
	     (match myRecv ch with
		| None -> mySend ch v1
		| v2 ->
		    let ch1 = mergeSort()
		    and ch2 = mergeSort()
		    in
		      begin
			mySend ch1 v1;
			mySend ch2 v2;
			split (ch, ch1, ch2);
			merge (ch1, ch2, ch)
		      end);
	     mySend ch None
	   end)
  in
    (spawn sort; ch)

  parent reply	other threads:[~2010-07-15 19:56 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-14 16:09 Goswin von Brederlow
2010-07-15 15:58 ` [Caml-list] " Rich Neswold
2010-07-15 16:19   ` David McClain
2010-07-15 17:16   ` Ashish Agarwal
2010-07-15 18:24   ` Goswin von Brederlow
2010-07-15 18:37     ` David McClain
2010-07-15 18:40     ` David McClain
2010-07-15 19:56     ` Rich Neswold [this message]
2010-07-16  4:02       ` Goswin von Brederlow
2010-07-16  4:23         ` Rich Neswold
2010-07-16 13:02           ` Goswin von Brederlow
2010-07-16 14:40             ` Dawid Toton
2010-07-16 16:18             ` [Caml-list] " Rich Neswold
2010-07-17 17:53               ` Eray Ozkural
2010-07-20  4:54             ` Satoshi Ogasawara
2010-07-17 18:34         ` Eray Ozkural
2010-07-17 19:35           ` Goswin von Brederlow
2010-07-17 22:00             ` Eray Ozkural
2010-07-15 16:32 ` Romain Beauxis
2010-07-15 17:46   ` Goswin von Brederlow
2010-07-15 18:44     ` Romain Beauxis
2010-07-16  3:52       ` Goswin von Brederlow
2010-07-16  4:19         ` Romain Beauxis
2010-07-16 13:05           ` Goswin von Brederlow
2010-07-16 13:20             ` Romain Beauxis
2010-07-17  9:07               ` Goswin von Brederlow
2010-07-17 13:51                 ` Romain Beauxis
2010-07-17 14:08                   ` Goswin von Brederlow
2010-07-17  9:52 ` Goswin von Brederlow
2010-07-17 14:20   ` Romain Beauxis
2010-07-17 15:52     ` Goswin von Brederlow

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=AANLkTik3C58pheUssb8LDtvj3gkcA3qoKT0YsaQ65B6N@mail.gmail.com \
    --to=rich.neswold@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=goswin-v-b@web.de \
    /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).