* [Caml-list] polymorphic stack
@ 2011-09-24 19:31 Walter Cazzola
2011-09-24 20:05 ` pierrchp
0 siblings, 1 reply; 3+ messages in thread
From: Walter Cazzola @ 2011-09-24 19:31 UTC (permalink / raw)
To: OCaML Mailing List
[-- Attachment #1: Type: TEXT/PLAIN, Size: 946 bytes --]
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
--
[-- Attachment #2: Type: TEXT/PLAIN, Size: 412 bytes --]
module Stack = struct
type 'a stack = { mutable c : 'a list }
exception EmptyStackException
let empty = { c = [] }
let push s x = s.c <- x :: s.c
let pop s =
match s.c with
hd::tl -> s.c <- tl
| [] -> raise EmptyStackException
let top s =
match s.c with
hd::_ -> hd
| [] -> raise EmptyStackException
let is_empty s = (s.c = [])
end;;
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] polymorphic stack
2011-09-24 19:31 [Caml-list] polymorphic stack Walter Cazzola
@ 2011-09-24 20:05 ` pierrchp
2011-09-24 20:46 ` Walter Cazzola
0 siblings, 1 reply; 3+ messages in thread
From: pierrchp @ 2011-09-24 20:05 UTC (permalink / raw)
To: Walter Cazzola; +Cc: OCaML Mailing List
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 <cazzola@dico.unimi.it>:
> 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
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] polymorphic stack
2011-09-24 20:05 ` pierrchp
@ 2011-09-24 20:46 ` Walter Cazzola
0 siblings, 0 replies; 3+ messages in thread
From: Walter Cazzola @ 2011-09-24 20:46 UTC (permalink / raw)
To: pierrchp; +Cc: OCaML Mailing List
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 <cazzola@dico.unimi.it>:
>
>> 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
>>
>>
>
--
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-09-24 20:46 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-24 19:31 [Caml-list] polymorphic stack Walter Cazzola
2011-09-24 20:05 ` pierrchp
2011-09-24 20:46 ` Walter Cazzola
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox