caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] One question
@ 2003-02-23 16:29 Alexander S. Usov
  2003-02-23 17:12 ` Samuel Mimram
  2003-02-23 20:59 ` Remi Vanicat
  0 siblings, 2 replies; 5+ messages in thread
From: Alexander S. Usov @ 2003-02-23 16:29 UTC (permalink / raw)
  To: OCaml mailing list

Hi!

Can anybody expalin me why in such code

------
type lex_node = Letter of char * bool * lex_tree
and  lex_tree = lex_node list ;;

exceprion Already ;;

let rec insert lex word =
  let whd = word.[0]
  and wtl = String.sub word 1 (String.length word - 1)
  in
    try
      match lex with
	| (Letter(c,b,t) as letter)::tail when (c <> whd) -> 
	    letter :: (insert tail word)
	| Letter(c,b,t) :: tail when (wtl = "") ->
	    if b = true then
	      raise Already
	    else
	      Letter(c,true,t) :: tail
	| Letter(c,b,t) :: tail ->
	    Letter(c,b,(insert t wtl)) :: tail
	| [] ->
	    if wtl = "" then
	      [ Letter (whd, true, []) ]
	    else
	      [ Letter (whd, false, insert [] wtl) ]
    with
      | Already -> lex ;;

let a = insert [] "word"
in
  a == insert a "word" ;;
-----

returns false?

-- 
Best regards,
  Alexander.


-------------------
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] 5+ messages in thread

* Re: [Caml-list] One question
  2003-02-23 16:29 [Caml-list] One question Alexander S. Usov
@ 2003-02-23 17:12 ` Samuel Mimram
  2003-02-23 17:28   ` Alexander S. Usov
  2003-02-23 20:59 ` Remi Vanicat
  1 sibling, 1 reply; 5+ messages in thread
From: Samuel Mimram @ 2003-02-23 17:12 UTC (permalink / raw)
  To: Alexander S. Usov; +Cc: caml-list

Hello.

I think you are confusing the operators = and ==. = is used to check if two objects have the same "logical" contents whether == checks if two objects are exactly the same in memory.
When you type a = insert a "word", the function insert returns a new lex_tree which has the same contents as the variable a but is another object in memory. That is why I think you wanted to write :

let a = insert [] "word"
in
a = insert a "word" ;;

which effectively returns true. It is important to understand that the variable a and the result returned by the function insert are logically the same but not physically.

Samuel.

On Sun, 23 Feb 2003 17:29:14 +0100
"Alexander S. Usov" <A.S.Usov@KVI.nl> wrote:

> Hi!
> 
> Can anybody expalin me why in such code
> 
> ------
> type lex_node = Letter of char * bool * lex_tree
> and  lex_tree = lex_node list ;;
> 
> exceprion Already ;;
> 
> let rec insert lex word =
>   let whd = word.[0]
>   and wtl = String.sub word 1 (String.length word - 1)
>   in
>     try
>       match lex with
> 	| (Letter(c,b,t) as letter)::tail when (c <> whd) -> 
> 	    letter :: (insert tail word)
> 	| Letter(c,b,t) :: tail when (wtl = "") ->
> 	    if b = true then
> 	      raise Already
> 	    else
> 	      Letter(c,true,t) :: tail
> 	| Letter(c,b,t) :: tail ->
> 	    Letter(c,b,(insert t wtl)) :: tail
> 	| [] ->
> 	    if wtl = "" then
> 	      [ Letter (whd, true, []) ]
> 	    else
> 	      [ Letter (whd, false, insert [] wtl) ]
>     with
>       | Already -> lex ;;
> 
> let a = insert [] "word"
> in
>   a == insert a "word" ;;
> -----
> 
> returns false?
> 
> -- 
> Best regards,
>   Alexander.
> 
> 
> -------------------
> 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


-- 
Samuel Mimram

Samuel.Mimram@ens-lyon.fr
-------------------
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] 5+ messages in thread

* Re: [Caml-list] One question
  2003-02-23 17:12 ` Samuel Mimram
@ 2003-02-23 17:28   ` Alexander S. Usov
  2003-02-23 17:49     ` Samuel Mimram
  0 siblings, 1 reply; 5+ messages in thread
From: Alexander S. Usov @ 2003-02-23 17:28 UTC (permalink / raw)
  To: Samuel Mimram; +Cc: OCaml mailing list

On Sun, 2003-02-23 at 18:12, Samuel Mimram wrote:
> Hello.
> 
> I think you are confusing the operators = and ==. = is used to check
> if two objects have the same "logical" contents whether == checks if 
> two objects are exactly the same in memory.
> When you type a = insert a "word", the function insert returns a new
> lex_tree which has the same contents as the variable a but is another
> object in memory. That is why I think you wanted to write :
> 
> let a = insert [] "word"
> in
> a = insert a "word" ;;
> 
> which effectively returns true. It is important to understand that the
> variable a and the result returned by the function insert are logically 
> the same but not physically.

Yes, I understand that == checks for a physical equality.
But in case the word is already present in tree I return the same
dictionary (note "raise Already" in insert). 
The idea was to make shure that the dictionary won't be modified if we
do not change anything in it.

-- 
Best regards,
  Alexander.

-------------------
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] 5+ messages in thread

* Re: [Caml-list] One question
  2003-02-23 17:28   ` Alexander S. Usov
@ 2003-02-23 17:49     ` Samuel Mimram
  0 siblings, 0 replies; 5+ messages in thread
From: Samuel Mimram @ 2003-02-23 17:49 UTC (permalink / raw)
  To: Alexander S. Usov; +Cc: caml-list

Sorry, I think I answered too quickly. The exception is effectively raised...

Samuel.

On Sun, 23 Feb 2003 18:28:31 +0100
"Alexander S. Usov" <A.S.Usov@KVI.nl> wrote:

> On Sun, 2003-02-23 at 18:12, Samuel Mimram wrote:
> > Hello.
> > 
> > I think you are confusing the operators = and ==. = is used to check
> > if two objects have the same "logical" contents whether == checks if 
> > two objects are exactly the same in memory.
> > When you type a = insert a "word", the function insert returns a new
> > lex_tree which has the same contents as the variable a but is another
> > object in memory. That is why I think you wanted to write :
> > 
> > let a = insert [] "word"
> > in
> > a = insert a "word" ;;
> > 
> > which effectively returns true. It is important to understand that the
> > variable a and the result returned by the function insert are logically 
> > the same but not physically.
> 
> Yes, I understand that == checks for a physical equality.
> But in case the word is already present in tree I return the same
> dictionary (note "raise Already" in insert). 
> The idea was to make shure that the dictionary won't be modified if we
> do not change anything in it.
> 
> -- 
> Best regards,
>   Alexander.
> 
> -------------------
> 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


-- 
Samuel Mimram

Samuel.Mimram@ens-lyon.fr
-------------------
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] 5+ messages in thread

* Re: [Caml-list] One question
  2003-02-23 16:29 [Caml-list] One question Alexander S. Usov
  2003-02-23 17:12 ` Samuel Mimram
@ 2003-02-23 20:59 ` Remi Vanicat
  1 sibling, 0 replies; 5+ messages in thread
From: Remi Vanicat @ 2003-02-23 20:59 UTC (permalink / raw)
  To: caml-list

"Alexander S. Usov" <A.S.Usov@KVI.nl> writes:

> Hi!
>
> Can anybody expalin me why in such code
>
> ------
> type lex_node = Letter of char * bool * lex_tree
> and  lex_tree = lex_node list ;;
>
> exceprion Already ;;
>
> let rec insert lex word =
>   let whd = word.[0]
>   and wtl = String.sub word 1 (String.length word - 1)
>   in
>     try
>       match lex with
> 	| (Letter(c,b,t) as letter)::tail when (c <> whd) -> 
> 	    letter :: (insert tail word)
> 	| Letter(c,b,t) :: tail when (wtl = "") ->
> 	    if b = true then
> 	      raise Already
> 	    else
> 	      Letter(c,true,t) :: tail
> 	| Letter(c,b,t) :: tail ->
> 	    Letter(c,b,(insert t wtl)) :: tail
> 	| [] ->
> 	    if wtl = "" then
> 	      [ Letter (whd, true, []) ]
> 	    else
> 	      [ Letter (whd, false, insert [] wtl) ]
>     with
>       | Already -> lex ;;
>
> let a = insert [] "word"
> in
>   a == insert a "word" ;;
> -----
>
> returns false?

a #trace insert make it very clear to me : it's because the Already
exception is immediately caught. your code do exactly the same than :

let rec insert lex word =
   let whd = word.[0]
   and wtl = String.sub word 1 (String.length word - 1)
   in
      match lex with
	| (Letter(c,b,t) as letter)::tail when (c <> whd) -> 
	    letter :: (insert tail word)
	| Letter(c,b,t) :: tail when (wtl = "") ->
	    if b = true then
	      lex
	    else
	      Letter(c,true,t) :: tail
	| Letter(c,b,t) :: tail ->
	    Letter(c,b,(insert t wtl)) :: tail
	| [] ->
	    if wtl = "" then
	      [ Letter (whd, true, []) ]
	    else
	      [ Letter (whd, false, insert [] wtl) ]


you should catch the exception at the top most part of the insertion.

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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] 5+ messages in thread

end of thread, other threads:[~2003-02-23 21:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-23 16:29 [Caml-list] One question Alexander S. Usov
2003-02-23 17:12 ` Samuel Mimram
2003-02-23 17:28   ` Alexander S. Usov
2003-02-23 17:49     ` Samuel Mimram
2003-02-23 20:59 ` Remi Vanicat

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).