* Re: [Caml-list] help
@ 2004-04-23 2:31 mohammad siddiqui
2004-04-23 4:17 ` Jon Harrop
2004-04-23 15:43 ` Brian Hurt
0 siblings, 2 replies; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-23 2:31 UTC (permalink / raw)
To: zilles; +Cc: skaller, rich, caml-list
I already made it mutable. words is an array of structure called "word". If
I try to change the value as you told it changes tha values of all the
elements of array "words". Thanks for your help in advance.
Mohammad S Siddiqui.
>From: Karl Zilles <zilles@1969.ws>
>To: mohammad siddiqui <writetosafi@hotmail.com>
>CC: skaller@users.sourceforge.net, rich@annexia.org, caml-list@inria.fr
>Subject: Re: [Caml-list] help
>Date: Thu, 22 Apr 2004 16:57:43 -0700
>
>mohammad siddiqui wrote:
>>Hello,
>>I am unable to change the values for an example How can I change the value
>>od
>>model.supvec[i].words[i].wnum?
>
>If you make wnum "mutable", you can just say:
>
>model.supvec.(i).words.(i).wnum <- newvalue
>
>although I suspect you don't really want use "i" twice.
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-23 2:31 [Caml-list] help mohammad siddiqui
@ 2004-04-23 4:17 ` Jon Harrop
2004-04-23 6:52 ` Jean-Christophe Filliatre
2004-04-23 15:43 ` Brian Hurt
1 sibling, 1 reply; 24+ messages in thread
From: Jon Harrop @ 2004-04-23 4:17 UTC (permalink / raw)
To: caml-list
On Friday 23 April 2004 3:31 am, mohammad siddiqui wrote:
> I already made it mutable. words is an array of structure called "word".
> If I try to change the value as you told it changes tha values of all the
> elements of array "words". Thanks for your help in advance.
>
> Mohammad S Siddiqui.
I haven't actually done that sort of thing myself but asking ocamlbrowser for
the interface information on Array.make gives:
>>>>>
external make : int -> 'a -> 'a array = "make_vect"
(** [Array.make n x] returns a fresh array of length [n],
initialized with [x].
All the elements of this new array are initially
physically equal to [x] (in the sense of the [==] predicate).
Consequently, if [x] is mutable, it is shared among all elements
of the array, and modifying [x] through one of the array entries
will modify all other entries at the same time.
Raise [Invalid_argument] if [n < 0] or [n > Sys.max_array_length].
If the value of [x] is a floating-point number, then the maximum
size is only [Sys.max_array_length / 2].*)
<<<<<
"Array.create" is a depricated alias for "Array.make", BTW.
So if you're using either "Array.make" or "Array.create" to build your array
then you're creating an array filled with references to the same thing. Does
that answer your question?
Personally, I tend to build up a list and then convert it into an array using
"Array.of_list". I guess the question is: are you creating an unecessary
level of indirection by making the type of the elements in your array mutable
when an array element is itself already mutable?
If you're still having trouble with your data structures then I'll run through
it with you off the mailing list...
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-23 4:17 ` Jon Harrop
@ 2004-04-23 6:52 ` Jean-Christophe Filliatre
0 siblings, 0 replies; 24+ messages in thread
From: Jean-Christophe Filliatre @ 2004-04-23 6:52 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
Jon Harrop writes:
>
> On Friday 23 April 2004 3:31 am, mohammad siddiqui wrote:
> > I already made it mutable. words is an array of structure called "word".
> > If I try to change the value as you told it changes tha values of all the
> > elements of array "words". Thanks for your help in advance.
>
> So if you're using either "Array.make" or "Array.create" to build
> your array then you're creating an array filled with references to
> the same thing. Does that answer your question?
It is likely to be the explanation. More precisely, you should write
Array.init n (fun _ -> { wnum = ...; weight = ... })
and not
Array.make n { wnum = ...; weight = ... }
In the latter case, all array cells point to the same value.
--
Jean-Christophe
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-23 2:31 [Caml-list] help mohammad siddiqui
2004-04-23 4:17 ` Jon Harrop
@ 2004-04-23 15:43 ` Brian Hurt
1 sibling, 0 replies; 24+ messages in thread
From: Brian Hurt @ 2004-04-23 15:43 UTC (permalink / raw)
To: mohammad siddiqui; +Cc: zilles, skaller, rich, Ocaml Mailing List
On Fri, 23 Apr 2004, mohammad siddiqui wrote:
> I already made it mutable. words is an array of structure called "word". If
> I try to change the value as you told it changes tha values of all the
> elements of array "words". Thanks for your help in advance.
You can't "inline" data structures into other data structures. The word
array you made is actually, in C terms, a word * array.
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
@ 2004-04-25 16:30 mohammad siddiqui
2004-04-25 1:53 ` mohammad siddiqui
` (2 more replies)
0 siblings, 3 replies; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-25 16:30 UTC (permalink / raw)
To: jdh30, caml-list
John,
I tried exactly what you have told. Its is fine when we just have one
element in an array of words. If we have more than one, changing the value
of one changes the values of the rest of the elements.
for example , if the test_model is initialiazed like this:
let test_model =
{ sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 2
{ docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
{ wnum=ref 0; weight=0. } }; alpha=0. };
now if i change the values , it chnages all th elements of the array
"supvec" in model
test_model.supvec.(0).words.(0).wnum := 1;
# test_model;;
- : model =
{sv_num = 0; at_upper_bound = 0; b = 0.;
supvec =
[|{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 1}; weight = 0.}|]};
{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 1}; weight = 0.}|]}|];
alpha = 0.}
Thanks,
Mohammad S Siddiqui.
it changes the values of wnum in all elements of model.supvec.
{ sv_num=0; at_upper_bound=0; b=0.; supvec=[
{ docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
{ wnum=ref 0; weight=0. } }; alpha=0. };
>From: Jon Harrop <jdh30@cam.ac.uk>
>To: caml-list@inria.fr
>Subject: Re: [Caml-list] help
>Date: Sun, 25 Apr 2004 13:13:37 +0100
>
>
>I sent you this as an e-mail on Friday:
>
>The following types are a start for your conversion:
>
>type fnum=int and fval=float;
>
>type word = { wnum: fnum ref; weight: fval }
>type doc = { docnum: int; queryid: int; costfactor: float; twonorm_sq:
>float;
>words: word array }
>type model = { sv_num: int; at_upper_bound: int; b: float; supvec: doc
>array;
>alpha: float };
>
>The following line creates an example "model" with its values filled in:
>
>let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 1
>{ docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
>{ wnum=ref 0; weight=0. } }; alpha=0. };
>
>I deliberately made "wnum" a reference so that it can be set:
>
>test_model.supvec.(0).words.(0).wnum := 1;
>
>If you want to do a really direct conversion then you probably want to make
>each of the fields in each of the records a reference (as I did with
>"wnum").
>However, you may wish to exercise the functional programming side of ocaml
>and write functions which replace data structures, instead of altering them
>in an imperative style. Although this is unintuitive at first, because an
>imperative language would do lots of copying and deleting of data
>structures,
>the ocaml compiler is very adept at performing only the changes which are
>made to a data structure...
>
>HTH.
>
>Cheers,
>Jon.
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives:
>http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
>http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
_________________________________________________________________
Test your Travel Quotient and get the chance to win your dream trip!
http://travel.msn.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* [Caml-list] help
@ 2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
2004-04-25 17:28 ` Matt Gushee
0 siblings, 2 replies; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-25 1:53 UTC (permalink / raw)
To: bhurt; +Cc: zilles, skaller, rich, caml-list
Hello,
I am having problem dealing with data structures in OCAML, I am translating
code from C to OCAML. As shown below one structure contains array of other
structures as its field. One of the guys here told me that we cant inline
structures into other structures in OCAML.
I dont how to deal with this, I already spent like 2 days on it. Should I
make use of objects and let some member of it array of other objects? I am
not sure this will do the purpose. I really appreciate if anyone helps me
out this.
For example if try to change the value model.supvec. (1).words. (1).wnum, it
changes the values of all elements of the array model.supvec. I tried using
references, mutable fields, Array.set function for modifying the contents of
individual element.
The structures in OCAML are:
type word = { mutable wnum:int; (* word number *)
mutable weight:float };; (* word weight *)
type doc = {
mutable docnum:int; (* Document ID *)
mutable queryid:
mutable costfactor:float;
mutable twonorm_sq:float;
mutable words:word array
};;
type model = {
mutable sv_num:int;
mutable at_upper_bound:int;
mutable b:float;
mutable supvec:doc array;
mutable alpha: int array;
} ;;
The Structures in C are
typedef struct word {
int wnum;
float weight;
} WORD;
typedef struct doc {
long docnum;
long queryid;
double costfactor;
double twonorm_sq;
WORD *words;
} DOC;
typedef struct model {
long sv_num;
long at_upper_bound;
double b;
DOC **supvec;
Double *alpha;
} MODEL;
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 1:53 ` mohammad siddiqui
@ 2004-04-25 12:13 ` Jon Harrop
2004-04-25 12:45 ` Henri DF
2004-04-25 17:28 ` Matt Gushee
1 sibling, 1 reply; 24+ messages in thread
From: Jon Harrop @ 2004-04-25 12:13 UTC (permalink / raw)
To: caml-list
I sent you this as an e-mail on Friday:
The following types are a start for your conversion:
type fnum=int and fval=float;
type word = { wnum: fnum ref; weight: fval }
type doc = { docnum: int; queryid: int; costfactor: float; twonorm_sq: float;
words: word array }
type model = { sv_num: int; at_upper_bound: int; b: float; supvec: doc array;
alpha: float };
The following line creates an example "model" with its values filled in:
let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 1
{ docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
{ wnum=ref 0; weight=0. } }; alpha=0. };
I deliberately made "wnum" a reference so that it can be set:
test_model.supvec.(0).words.(0).wnum := 1;
If you want to do a really direct conversion then you probably want to make
each of the fields in each of the records a reference (as I did with "wnum").
However, you may wish to exercise the functional programming side of ocaml
and write functions which replace data structures, instead of altering them
in an imperative style. Although this is unintuitive at first, because an
imperative language would do lots of copying and deleting of data structures,
the ocaml compiler is very adept at performing only the changes which are
made to a data structure...
HTH.
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 12:13 ` Jon Harrop
@ 2004-04-25 12:45 ` Henri DF
2004-04-26 19:13 ` Jon Harrop
0 siblings, 1 reply; 24+ messages in thread
From: Henri DF @ 2004-04-25 12:45 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
> I sent you this as an e-mail on Friday:
>
> The following types are a start for your conversion:
>
> type fnum=int and fval=float;
>
> type word = { wnum: fnum ref; weight: fval }
> type doc = { docnum: int; queryid: int; costfactor: float; twonorm_sq: float;
> words: word array }
> type model = { sv_num: int; at_upper_bound: int; b: float; supvec: doc array;
> alpha: float };
>
> The following line creates an example "model" with its values filled in:
>
> let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 1
> { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
> { wnum=ref 0; weight=0. } }; alpha=0. };
>
> I deliberately made "wnum" a reference so that it can be set:
any reason not to make it a mutable ?
henri
> test_model.supvec.(0).words.(0).wnum := 1;
>
> If you want to do a really direct conversion then you probably want to make
> each of the fields in each of the records a reference (as I did with "wnum").
> However, you may wish to exercise the functional programming side of ocaml
> and write functions which replace data structures, instead of altering them
> in an imperative style. Although this is unintuitive at first, because an
> imperative language would do lots of copying and deleting of data structures,
> the ocaml compiler is very adept at performing only the changes which are
> made to a data structure...
>
> HTH.
>
> Cheers,
> Jon.
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 12:45 ` Henri DF
@ 2004-04-26 19:13 ` Jon Harrop
2004-04-26 22:38 ` Andrew Lenharth
0 siblings, 1 reply; 24+ messages in thread
From: Jon Harrop @ 2004-04-26 19:13 UTC (permalink / raw)
To: caml-list
On Sunday 25 April 2004 1:45 pm, Henri DF wrote:
> > I deliberately made "wnum" a reference so that it can be set:
>
> any reason not to make it a mutable ?
No good reason. ;)
I've just been looking at the assembler output from ocamlopt on this. I think
that a mutable record field represents one fewer levels of indirection
compared to a reference record field. On a contrived example, using mutable
is twice as fast.
Comparing a mutable record field to a reference (not in a record), the latter
comes out as slightly quicker in my timings. But I think this is
insignificant as the assembler output seems to be identical.
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-26 19:13 ` Jon Harrop
@ 2004-04-26 22:38 ` Andrew Lenharth
0 siblings, 0 replies; 24+ messages in thread
From: Andrew Lenharth @ 2004-04-26 22:38 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
On Mon, Apr 26, 2004 at 08:13:42PM +0100, Jon Harrop wrote:
> On Sunday 25 April 2004 1:45 pm, Henri DF wrote:
> > > I deliberately made "wnum" a reference so that it can be set:
> >
> > any reason not to make it a mutable ?
>
> No good reason. ;)
Take a look at how it is implemented in Pervasives.
>From the manual:
type 'a ref = {
mutable contents : 'a;
}
So, yea, unless you like another layer of indirection, just using
mutable should save you a little bit.
Andrew
--
"It will work in practice, yes. But will it work in theory?"
--- a french diplomat's comment, recalled by Madeleine Albright
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
@ 2004-04-25 17:28 ` Matt Gushee
1 sibling, 0 replies; 24+ messages in thread
From: Matt Gushee @ 2004-04-25 17:28 UTC (permalink / raw)
To: caml-list
On Sun, Apr 25, 2004 at 01:53:48AM +0000, mohammad siddiqui wrote:
> Hello,
>
> I am having problem dealing with data structures in OCAML, I am translating
> code from C to OCAML. As shown below one structure contains array of other
> structures as its field. One of the guys here told me that we can?t
> ?inline? structures into other structures in OCAML.
> I don?t how to deal with this, I already spent like 2 days on it. Should I
> make use of objects and let some member of it array of other objects? I am
> not sure this will do the purpose. I really appreciate if anyone helps me
> out this.
>
> For example if try to change the value model.supvec. (1).words. (1).wnum,
> it changes the values of all elements of the array model.supvec. I tried
> using references, mutable fields, Array.set function for modifying the
> contents of individual element.
>
> The structures in OCAML are:
>
> type word = { mutable wnum:int; (* word number *)
> mutable weight:float };; (* word weight *)
>
> type doc = {
> mutable docnum:int; (* Document ID *)
> mutable queryid:
> mutable costfactor:float;
> mutable twonorm_sq:float;
> mutable words:word array
> };;
>
> type model = {
> mutable sv_num:int;
> mutable at_upper_bound:int;
> mutable b:float;
> mutable supvec:doc array;
> mutable alpha: int array;
>
> } ;;
>
> The Structures in C are
>
> typedef struct word {
> int wnum;
> float weight;
> } WORD;
>
> typedef struct doc {
> long docnum;
> long queryid;
> double costfactor;
> double twonorm_sq;
> WORD *words;
> } DOC;
>
>
>
> typedef struct model {
> long sv_num;
> long at_upper_bound;
> double b;
> DOC **supvec;
> Double *alpha;
> } MODEL;
>
> _________________________________________________________________
> FREE pop-up blocking with the new MSN Toolbar ? get it now!
> http://toolbar.msn.com/go/onm00200415ave/direct/01/
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
On Sun, Apr 25, 2004 at 04:30:56PM +0000, mohammad siddiqui wrote:
> I tried exactly what you have told. Its is fine when we just have one
> element in an array of words. If we have more than one, changing the value
> of one changes the values of the rest of the elements.
That's not exactly what's happening, though your example makes it look
that way.
> for example , if the test_model is initialiazed like this:
>
> let test_model =
> { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 2
> { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
> { wnum=ref 0; weight=0. } }; alpha=0. };
Okay, we can see the cause of the problem here: you are initializing an
array with Array.make, where the initial value includes a ref. The
resulting array will have unique members, *except* that each 'wnum'
field points to a single, shared ref value. That's the problem.
> now if i change the values , it chnages all th elements of the array
> "supvec" in model
>
> test_model.supvec.(0).words.(0).wnum := 1;
As you probably understand by now, what this is really doing is changing
all instances of the shared value stored in the 'wnum' field. Evidently
that was the only field you tried to change, in which case it would
appear that all the elements of 'supvec' were being changed.
Here are three possible solutions:
1. Make wnum a mutable field instead of a ref.
2. Replace the array member instead of setting the ref:
test_model.supvec.(0).words <- newvalue
(but then I guess there's no point in 'wnum' being either a ref or
a mutable field)
3. Don't use Array.make to initialize the array. Instead, use a
recursive function that starts with an empty array, and adds new
elements with Array.append.
--
Matt Gushee When a nation follows the Way,
Englewood, Colorado, USA Horses bear manure through
mgushee@havenrock.com its fields;
http://www.havenrock.com/ When a nation ignores the Way,
Horses bear soldiers through
its streets.
--Lao Tzu (Peter Merel, trans.)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 16:30 mohammad siddiqui
2004-04-25 1:53 ` mohammad siddiqui
@ 2004-04-25 17:06 ` Jon Harrop
2004-04-25 17:39 ` Matt Gushee
2004-04-26 0:45 ` skaller
2 siblings, 1 reply; 24+ messages in thread
From: Jon Harrop @ 2004-04-25 17:06 UTC (permalink / raw)
To: caml-list
On Sunday 25 April 2004 5:30 pm, mohammad siddiqui wrote:
> I tried exactly what you have told. Its is fine when we just have one
> element in an array of words. If we have more than one, changing the value
> of one changes the values of the rest of the elements.
Sorry, yes. Now I come to think of it, it's kind of obvious that a mutable
field in a record would also get copied. My fault. So we don't want to use
Array.make to generate this structure...
How about using Array.init? This function takes the size of the array and a
function which generates an element in the array given the index of the
element within the array, e.g.:
# Array.init 4 (fun i -> i*i);;
- : int array = [|0; 1; 4; 9|]
So we get:
let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.init 4 (fun
i -> { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=[| { wnum=ref
(i*i); weight=0. } |] }); alpha=0. };;
val test_model : model =
{sv_num = 0; at_upper_bound = 0; b = 0.;
supvec =
[|{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 0}; weight = 0.}|]};
{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 1}; weight = 0.}|]};
{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 4}; weight = 0.}|]};
{docnum = 0; queryid = 0; costfactor = 0.; twonorm_sq = 0.;
words = [|{wnum = {contents = 9}; weight = 0.}|]}|];
alpha = 0.}
HTH!
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 17:06 ` Jon Harrop
@ 2004-04-25 17:39 ` Matt Gushee
0 siblings, 0 replies; 24+ messages in thread
From: Matt Gushee @ 2004-04-25 17:39 UTC (permalink / raw)
To: caml-list
On Sun, Apr 25, 2004 at 06:06:23PM +0100, Jon Harrop wrote:
> On Sunday 25 April 2004 5:30 pm, mohammad siddiqui wrote:
> > I tried exactly what you have told. Its is fine when we just have one
> > element in an array of words. If we have more than one, changing the value
> > of one changes the values of the rest of the elements.
>
> Sorry, yes. Now I come to think of it, it's kind of obvious that a mutable
> field in a record would also get copied.
Yes, a mutable field *or* a ref (which invalidates one of the solutions
I suggested). Actually, a ref is just a predefined record type that has a
mutable field and is supported with a special assignment operator.
# let wnum = { contents = 0 };;
val wnum : int ref = {contents = 0}
--
Matt Gushee When a nation follows the Way,
Englewood, Colorado, USA Horses bear manure through
mgushee@havenrock.com its fields;
http://www.havenrock.com/ When a nation ignores the Way,
Horses bear soldiers through
its streets.
--Lao Tzu (Peter Merel, trans.)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-25 16:30 mohammad siddiqui
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 17:06 ` Jon Harrop
@ 2004-04-26 0:45 ` skaller
2 siblings, 0 replies; 24+ messages in thread
From: skaller @ 2004-04-26 0:45 UTC (permalink / raw)
To: mohammad siddiqui; +Cc: jdh30, caml-list
On Mon, 2004-04-26 at 02:30, mohammad siddiqui wrote:
> John,
>
> I tried exactly what you have told. Its is fine when we just have one
> element in an array of words. If we have more than one, changing the value
> of one changes the values of the rest of the elements.
I suggest you first try a *purely functional* approach.
The reason is: its easier! It will work exactly as you expect,
no suprises .. I promise <g>.
Get rid of references and mutable fields. Forget efficiency
concerns at this time.
Here is how to make a doc, containing words, where i have
simplified the data structures to demonstrate:
type wrd = string
type doc = { id:int; words: wrd list }
let add_word d w= { d with words = w::d.words }
(* NOTE: functional update syntactic sugar saves
explicitly copying each field .. unmentioned fields
get copied automatically *)
let empty = { id=99; words=[] }
let d = ref empty
;;
List.iter
(fun w -> d := add_word !d w)
["Hello";"This";"is";"a";"Document";"of";"words"]
;;
List.iter print_endline (!d.words)
;;
Note here i AM using a reference to hold the doc,
but the technique of adding a word to a doc
is purely functional: you get a brand new doc
for every word you add.
EXERCISE: use a fold to get rid of the 'd' variable,
making this little program *purely* functional
(up to the printing part anyhow :D
QUESTION: how fast is the functional technique
compared with using mutable fields or references
to modify a data structure in place??
I don't know the answer . but I'm going to GUESS.
The functional technique is 10% FASTER!
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* [Caml-list] help
@ 2004-04-22 23:34 mohammad siddiqui
2004-04-22 23:57 ` Karl Zilles
0 siblings, 1 reply; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-22 23:34 UTC (permalink / raw)
To: skaller; +Cc: rich, caml-list
Hello,
I am involved in a conversion project, where we are translating programs
from C
to ocaml. I am having hardtime dealing with pointers. One of the structures
contains a pointer to a structure as one of its fields, which in turns
contains
a pointer to another structure, I replaced this by using arrays in ocaml but
I
am not able to make changes to inner structures fields (using Array.set ) if
i
do so it changes the values of all the elements of that array.
Can any one help resolving this? The structures in C are below:
typedef struct word {
FNUM wnum; /* word number */
FVAL weight; /* word weight */
} WORD;
typedef struct doc {
long docnum; /* Document ID. This has to be the position
of
the document in the training set array. */
long queryid;
double costfactor;
double twonorm_sq;
WORD *words; /* The words/values in the document by */
/*increasing word-number. I replaced
this by
an array in ocaml*/
} DOC;
typedef struct model {
long sv_num;
long at_upper_bound;
double b;
DOC **supvec; /* I replaced this by array of type doc in ocaml */
double *alpha;
} MODEL;
I am unable to change the values for an example How can I change the value
od
model.supvec[i].words[i].wnum?
Thanks,
Mohammad S Siddiqui.
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
@ 2004-04-16 18:04 mohammad siddiqui
2004-04-16 18:59 ` skaller
0 siblings, 1 reply; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-16 18:04 UTC (permalink / raw)
To: rich; +Cc: caml-list
Hi,
I started the conversion project after completely getting hold of ocaml but
I still have some concerns like how we can manage pinters and pointer to
pointers, some functions in C like exit(), isspace().
The project is to convert a tool used in text classification from C to
Coaml.
Thanks,
Mohammad S Siddiqui
>From: Richard Jones <rich@annexia.org>
>To: mohammad siddiqui <writetosafi@hotmail.com>
>CC: caml-list@inria.fr
>Subject: Re: [Caml-list] help
>Date: Fri, 9 Apr 2004 10:04:37 +0100
>
>On Thu, Apr 08, 2004 at 10:57:26PM +0000, mohammad siddiqui wrote:
> > I am new to OCAML. I Just started learning. I need to convert the source
> > code of an applicaton which runs in 4000 lines from C to OCAML. Can
>anyone
> > help me how to get started? is there any good online resource which may
> > help conveting the code? i s there any tool available which does this?
> > Since it is a functional language, it is rather difficult to replace
> > constructs like loops with recursive functions and assignments
>statements
> > of structured programming language like 'C'.
>
>4,000 lines of C isn't a lot of code. Perhaps it's better to do it by
>hand rather than looking for an automatic tool?
>
>Rich.
>
>--
>Richard Jones. http://www.annexia.org/ http://www.j-london.com/
>Merjis Ltd. http://www.merjis.com/ - improving website return on investment
>MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
>RPMs, pkgs etc. Linux, BSD, Solaris.
>http://www.annexia.org/freeware/makeplus/
>
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives:
>http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
>http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-16 18:04 mohammad siddiqui
@ 2004-04-16 18:59 ` skaller
2004-04-17 0:34 ` Shawn Wagner
0 siblings, 1 reply; 24+ messages in thread
From: skaller @ 2004-04-16 18:59 UTC (permalink / raw)
To: mohammad siddiqui; +Cc: rich, caml-list
On Sat, 2004-04-17 at 04:04, mohammad siddiqui wrote:
> Hi,
> I started the conversion project after completely getting hold of ocaml but
> I still have some concerns like how we can manage pinters and pointer to
> pointers,
Generally you don't need them. Here is an example: in C:
struct ilist { list *next; int v; };
In Ocaml:
type ilist = Cons of int * ilist | Empty
The * there means 'tuple' not pointer, in Ocaml
everything except an int is already a pointer.
> some functions in C like exit(), isspace().
exception Exit
The Exit exception is not raised by any library function. It is provided
for use in your programs.
isspace is easy to implement:
let isspace = function
| ' ' | '\t' -> true
| _ -> false
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-16 18:59 ` skaller
@ 2004-04-17 0:34 ` Shawn Wagner
0 siblings, 0 replies; 24+ messages in thread
From: Shawn Wagner @ 2004-04-17 0:34 UTC (permalink / raw)
To: caml-list
On Sat, Apr 17, 2004 at 04:59:45AM +1000, skaller wrote:
> On Sat, 2004-04-17 at 04:04, mohammad siddiqui wrote:
> > Hi,
> > I started the conversion project after completely getting hold of ocaml but
> > I still have some concerns like how we can manage pinters and pointer to
> > pointers,
>
> Generally you don't need them. Here is an example: in C:
>
> struct ilist { list *next; int v; };
>
> In Ocaml:
>
> type ilist = Cons of int * ilist | Empty
This seems like a bad example, as you're just reinventing int list with it.
C pointers are something like ocaml references.
C: int *foo = &bar;
*foo = 12;
printf("%d", *foo);
Ocaml: let foo = ref bar in
foo := 12;
Printf.printf "%d" !foo
except that the value of bar won't be changed in the latter.
>
> > some functions in C like exit(), isspace().
>
> exception Exit
> The Exit exception is not raised by any library function. It is provided
> for use in your programs.
There's also the exit function, which does the same thing as C exit(), and
doesn't cause a message about an uncaught exception...
>
> isspace is easy to implement:
>
> let isspace = function
> | ' ' | '\t' -> true
> | _ -> false
\r, \n and some other characters also count as whitespace. The C character
classification routines (isspace, isalpha, etc.) are part of extlib.
--
Shawn Wagner
shawnw@speakeasy.org
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* [Caml-list] help
@ 2004-04-08 22:57 mohammad siddiqui
2004-04-09 6:56 ` Basile STARYNKEVITCH
2004-04-09 9:04 ` Richard Jones
0 siblings, 2 replies; 24+ messages in thread
From: mohammad siddiqui @ 2004-04-08 22:57 UTC (permalink / raw)
To: caml-list
I am new to OCAML. I Just started learning. I need to convert the source
code of an applicaton which runs in 4000 lines from C to OCAML. Can anyone
help me how to get started? is there any good online resource which may help
conveting the code? i s there any tool available which does this?
Since it is a functional language, it is rather difficult to replace
constructs like loops with recursive functions and assignments statements of
structured programming language like 'C'.
Thanks,
Siddiqui.
_________________________________________________________________
Is your PC infected? Get a FREE online computer virus scan from McAfee®
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-08 22:57 mohammad siddiqui
@ 2004-04-09 6:56 ` Basile STARYNKEVITCH
2004-04-09 9:04 ` Richard Jones
1 sibling, 0 replies; 24+ messages in thread
From: Basile STARYNKEVITCH @ 2004-04-09 6:56 UTC (permalink / raw)
To: mohammad siddiqui, caml-list
On Thu, Apr 08, 2004 at 10:57:26PM +0000, mohammad siddiqui wrote:
> I am new to OCAML. I Just started learning. I need to convert the
> source code of an application which runs in 4000 lines from C to
> OCAML.
You didn't tell use what kind of application is it...
> Can anyone help me how to get started?
I tend to believe that such a conversion is primarily a waste of
time.... And more importantly, if you convert blindly, you will get
ugly (and hardly maintainable) Ocaml code. You'll probably get an
Ocaml program as big as the original C source.
A more practical alternative might be to consider your lower C
routines as building blocks, ie to interface each of your (few) lower
C routines to Ocaml, and to recode the higher part of the application
in Ocaml.
You could also try to implement the same algorithms in Ocaml.
For completeness, you could translate your C code into an ugly,
unmaintainable Ocaml mess using references for C variables and mutable
fields all over the place. But the code will be shameful and you won't
learn anything doing it.
I strongly suggest - if you want to learn Ocaml :
first, to learn to code thru some basic (100 lines) programs -
learning how to use tail recursion and avoiding mutable data (and more
generally limiting side effects).
then, reimplement your 4000 C line program, starting from a
highlevel abstract view of the algorithm (not from the C source);
I'll bet you should get an Ocaml program smaller than 2000 lines.
There are many interesting books & online resources to learn Ocaml.
Starting by translating a C program into any better language is a bad way
to learn this better language (whatever the better language is).
Regards.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] help
2004-04-08 22:57 mohammad siddiqui
2004-04-09 6:56 ` Basile STARYNKEVITCH
@ 2004-04-09 9:04 ` Richard Jones
1 sibling, 0 replies; 24+ messages in thread
From: Richard Jones @ 2004-04-09 9:04 UTC (permalink / raw)
To: mohammad siddiqui; +Cc: caml-list
On Thu, Apr 08, 2004 at 10:57:26PM +0000, mohammad siddiqui wrote:
> I am new to OCAML. I Just started learning. I need to convert the source
> code of an applicaton which runs in 4000 lines from C to OCAML. Can anyone
> help me how to get started? is there any good online resource which may
> help conveting the code? i s there any tool available which does this?
> Since it is a functional language, it is rather difficult to replace
> constructs like loops with recursive functions and assignments statements
> of structured programming language like 'C'.
4,000 lines of C isn't a lot of code. Perhaps it's better to do it by
hand rather than looking for an automatic tool?
Rich.
--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
RPMs, pkgs etc. Linux, BSD, Solaris. http://www.annexia.org/freeware/makeplus/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 24+ messages in thread
* [Caml-list] help
@ 2002-11-05 12:22 ÀîÒÇ
2002-11-05 13:39 ` Alessandro Baretta
0 siblings, 1 reply; 24+ messages in thread
From: ÀîÒÇ @ 2002-11-05 12:22 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 168 bytes --]
could anybody tell me what does "self type can not escape its class" mean?
my function is
let test (mat: #seq array) = let result = Array.make 10 mat.(0) in result
[-- Attachment #2: Type: text/html, Size: 772 bytes --]
^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2004-04-26 22:38 UTC | newest]
Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-23 2:31 [Caml-list] help mohammad siddiqui
2004-04-23 4:17 ` Jon Harrop
2004-04-23 6:52 ` Jean-Christophe Filliatre
2004-04-23 15:43 ` Brian Hurt
-- strict thread matches above, loose matches on Subject: below --
2004-04-25 16:30 mohammad siddiqui
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
2004-04-25 12:45 ` Henri DF
2004-04-26 19:13 ` Jon Harrop
2004-04-26 22:38 ` Andrew Lenharth
2004-04-25 17:28 ` Matt Gushee
2004-04-25 17:06 ` Jon Harrop
2004-04-25 17:39 ` Matt Gushee
2004-04-26 0:45 ` skaller
2004-04-22 23:34 mohammad siddiqui
2004-04-22 23:57 ` Karl Zilles
2004-04-16 18:04 mohammad siddiqui
2004-04-16 18:59 ` skaller
2004-04-17 0:34 ` Shawn Wagner
2004-04-08 22:57 mohammad siddiqui
2004-04-09 6:56 ` Basile STARYNKEVITCH
2004-04-09 9:04 ` Richard Jones
2002-11-05 12:22 ÀîÒÇ
2002-11-05 13:39 ` Alessandro Baretta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox