From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8OKkin4024506 for ; Sat, 24 Sep 2011 22:46:44 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AswCAGxAfk6flYLKl2dsb2JhbABDmTE0jjkUAQEBAQEIFgc6gVMBAQQBOAI0CwULCyUhRRIZEodmAgS3T4MYg3MEk1KFEBKMEw X-IronPort-AV: E=Sophos;i="4.68,436,1312149600"; d="scan'208";a="121278663" Received: from smtp.dico.unimi.it ([159.149.130.202]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 24 Sep 2011 22:46:38 +0200 Received: from [192.168.1.100] (dynamic-adsl-94-36-56-5.clienti.tiscali.it [94.36.56.5]) (authenticated bits=0) by smtp.dico.unimi.it (8.14.4/8.14.4) with ESMTP id p8OKkY0V018225 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 24 Sep 2011 22:46:35 +0200 Date: Sat, 24 Sep 2011 22:46:29 +0200 (CEST) From: Walter Cazzola To: pierrchp@free.fr cc: OCaML Mailing List In-Reply-To: <1316894754.4e7e38225d52e@imp.free.fr> Message-ID: References: <1316894754.4e7e38225d52e@imp.free.fr> User-Agent: Alpine 2.00 (LFD 1167 2008-08-23) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0 (smtp.dico.unimi.it [159.149.130.202]); Sat, 24 Sep 2011 22:46:35 +0200 (CEST) X-DSI-MailScanner-Information: Please contact the staff for more information X-DSI-MailScanner-ID: p8OKkY0V018225 X-DSI-MailScanner: Found to be clean X-DSI-MailScanner-From: cazzola@dico.unimi.it Subject: Re: [Caml-list] polymorphic stack Hello, thanks a lot you got the point; I will never see that empty wasn't a function but just a constant name. Thanks also for the clear explanation. Cheers Walter On Sat, 24 Sep 2011, pierrchp@free.fr wrote: > Hello, > > Your problem here is that when you created your module stack, you wrote > > let empty = { c=[]} ;; > > which creates a global variable. Writing > > let s = empty > > creates a new stack by copying the value empty, therefore putting a type > constraint, that s and empty must have the same type. In order to do what you > want, you need to reevaluate the expression { c=[]} every time you create a new > stack, and not just copy its result. This is achieved by using a function > instead of a global variable: > > let empty () = { c=[]} > > an create a new stack with > > let s = empty () > > Hope it is not too confusing > > Cheers > > -Pierre > > Selon Walter Cazzola : > >> Hi all, >> thanks a lot for your help in this travel through OCaML but I have still >> a question. I have tried to write a polymorphic stack code attached but >> I don't understand its behavior: >> >> # open Stack;; >> # let s = empty;; >> val s : '_a Stack.stack = {c = []} >> # push s 7;; >> - : unit = () >> # push s 25;; >> - : unit = () >> # let s1 = empty;; >> val s1 : int Stack.stack = {c = [25; 7]} >> # push s1 "Hello";; >> Error: This expression has type string but an expression was expected of >> type >> int >> >> Apparently seems that I can have only a variable of type stack and any >> other call to its constructor links the new variable to the old one. >> This means that once I have put an int inside I can't have a second >> stack for characters or what else? This behavior is completely >> unexpected and I can't explain it. >> >> I'm sure I'm doing something wrong but I can't say what it is. Do you >> have any idea about? >> >> TIA >> Walter >> >> -- >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa-roc.inria.fr/wws/info/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> >> > --