From: Walter Cazzola <cazzola@dico.unimi.it>
To: OCaML Mailing List <caml-list@inria.fr>
Subject: [Caml-list] polymorphic stack
Date: Sat, 24 Sep 2011 21:31:54 +0200 (CEST) [thread overview]
Message-ID: <alpine.LFD.2.00.1109242125220.2944@surtur.dico.unimi.it> (raw)
[-- 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;;
next reply other threads:[~2011-09-24 19:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-24 19:31 Walter Cazzola [this message]
2011-09-24 20:05 ` pierrchp
2011-09-24 20:46 ` Walter Cazzola
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=alpine.LFD.2.00.1109242125220.2944@surtur.dico.unimi.it \
--to=cazzola@dico.unimi.it \
--cc=caml-list@inria.fr \
/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